【问题标题】:Xamarin Sharing A Base Android and iOS Project with multiple Android and iOS appsXamarin 与多个 Android 和 iOS 应用程序共享基础 Android 和 iOS 项目
【发布时间】:2018-05-22 23:33:52
【问题描述】:

我设置了一个项目,以便我可以为每个客户提供不同风格的应用程序,并且我正在尝试简化我们的设计。 目前它已经设置好了,所以我们有我们的 PCL,然后是每个客户端的 Android 和 iOS 项目。这意味着如果我们更改应用端代码的任何内容,我们必须在每个项目中手动更改它。

我一直在尝试对其进行更改,以便我们拥有我们的 PCL 和一个基本的 Android 和 iOS 项目,该项目具有平台特定的界面代码,其他每个项目及其各自的颜色样式、图像和包标识符都可以继承。

例如

  • PCL 项目(共享代码、表单等)
  • Android.Shared(Android 应用程序/平台特定代码的共享资源/本地化)
  • Android.Client1Theme(Client1 的自定义资源(徽标、颜色等))
  • Android.Client2Theme(Client2 的自定义资源(徽标、颜色等))

  • iOS.Shared(Android 应用程序/平台特定代码的共享资源/本地化)

  • iOS.Client1Theme(Client1 的自定义资源(徽标、颜色等))
  • iOS.Client2Theme(Client2 的自定义资源(徽标、颜色等))

我只需创建一个新的 android 项目并添加对共享 android 项目和 PCL 的引用,就可以在 Android 上使用它。

我无法以同样的方式为 iOS 工作。很接近了,iOS 应用程序编译为具有正确样式的新应用程序,但是 DependencyService 在尝试访问接口方法时崩溃。

DependencyService.Get<ISystemFunctions>().ToggleTorch();

exception thrown on iOS interface access

下面是我在共享项目中的接口代码,但我认为这不是问题,而是 DependencyService 找不到它。

[assembly: Dependency(typeof(SystemFunctions_iOS))]
namespace SharedApp.iOS.iOS_Interfaces
{
    public class SystemFunctions_iOS : ISystemFunctions
    {        
        public SystemFunctions_iOS()
        {   }

        public void ToggleTorch()
        {
            var device = AVCaptureDevice.GetDefaultDevice(AVMediaTypes.Video);
            if (device == null)
                return;

            device.LockForConfiguration(out NSError error);
            if (error != null)
            {
                device.UnlockForConfiguration();
                return;
            }
            else
            {
                device.TorchMode = device.TorchMode == AVCaptureTorchMode.On ? AVCaptureTorchMode.Off : AVCaptureTorchMode.On;
                device.UnlockForConfiguration();
            }
        }
    }
}

如果还有什么我可以分享的帮助,请告诉我。

【问题讨论】:

  • 同样的 DependencyService.Get<ISystemFunctions>() 方法在你的 Android 项目中是否有效?
  • 是的,它在 iOS 上运行良好。我找到了我在下面发布的问题的解决方案
  • * 我的意思是在 android 上运行良好。

标签: android ios xamarin dependency-injection shared-project


【解决方案1】:

找到解决我问题的方法

在 AppDelegate for iOS.Shared 中删除该行 [Register("AppDelegate")]

//[Register("AppDelegate")]   NEEDED TO REMOVE THIS
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{        
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());            

        return base.FinishedLaunching(app, options);
    }
}

在 iOS.ClientThemeX 的 AppDelegate 中添加该行 var systemFunctions_iOS = new iOS.Shared.iOS_Interfaces.SystemFunctions_iOS();

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());

        // DO NOT REMOVE THIS. It breaks without it. Don't know why
        // but a similar row will need to be added for each interface added to the project
        var systemFunctions_iOS = new SharedApp.iOS.iOS_Interfaces.SystemFunctions_iOS();

        return base.FinishedLaunching(app, options);
    }
}

从共享项目中删除 AppDelegate 寄存器是有意义的,但我不确定为什么我需要使用我的接口声明一个新对象才能工作。 这个接口构造函数,和问题一样,是空的。

如果有人仍然可以对此有所了解,我们将不胜感激,或者如果有任何建议可以更简单地执行此操作。

【讨论】:

    猜你喜欢
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多