简体   繁体   English

Xamarin与多个Android和iOS应用共享一个基础Android和iOS项目

[英]Xamarin Sharing A Base Android and iOS Project with multiple Android and iOS apps

I have a project set up so that I can have a different styled app for each of our clients and i'm trying to stream line our design. 我设置了一个项目,以便可以为每个客户使用不同样式的应用程序,而我正在尝试简化设计。 Currently it is set up so we have our PCL and then an Android and an iOS project for each client. 目前已经设置好了,因此我们为每个客户端提供了PCL,然后有一个Android和一个iOS项目。 This means that if we change anything on the app side code we have to change it manually across each project. 这意味着,如果我们在应用程序端代码上进行任何更改,则必须在每个项目中手动进行更改。

I have been attempting to change it so we have our PCL and a base Android and iOS project which has the platform specific interface code which each of the other projects with their individual colour styling, images and bundle identifiers can then inherit. 我一直在尝试对其进行更改,因此我们拥有PCL以及一个基本的Android和iOS项目,该项目具有特定于平台的界面代码,然后每个其他项目都可以继承各自的颜色样式,图像和包标识符。

eg 例如

  • PCL Poject (shared code, Forms, etc) PCL Poject(共享代码,表单等)
  • Android.Shared (shared resources/localisations for the android apps/platform specific code) Android.Shared(Android应用程序/平台特定代码的共享资源/本地化)
  • Android.Client1Theme (custom resources (logos, colors, etc) for Client1) Android.Client1Theme(Client1的自定义资源(徽标,颜色等))
  • Android.Client2Theme (custom resources (logos, colors, etc) for Client2) Android.Client2Theme(Client2的自定义资源(徽标,颜色等))

  • iOS.Shared (shared resources/localisations for the android apps/platform specific code) iOS.Shared(共享资源/ Android应用程序的本地化/平台特定的代码)

  • iOS.Client1Theme (custom resources (logos, colors, etc) for Client1) iOS.Client1Theme(Client1的自定义资源(徽标,颜色等))
  • iOS.Client2Theme (custom resources (logos, colors, etc) for Client2) iOS.Client2Theme(Client2的自定义资源(徽标,颜色等))

I have gotten this to work for Android simply by creating a new android project and adding a reference to the shared android project and PCL. 我已经通过创建一个新的android项目并添加对共享的android项目和PCL的引用,使它适用于Android。

I am having trouble getting to work for iOS in the same way. 我无法以相同的方式为iOS工作。 It's close, the iOS app compiles as a new app with the correct styling however the DependencyService crashes whenever it tries to access an interface method. 即将结束,iOS应用程序将作为具有正确样式的新应用程序编译,但是DependencyService在尝试访问接口方法时会崩溃。

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

exception thrown on iOS interface access iOS界面访问引发异常

Below is the code of my interface in my shared project, however i don't think this is the issue but rather the DependencyService cant find it. 下面是我共享项目中接口的代码,但是我不认为这是问题,而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();
            }
        }
    }
}

Let me know if there is anything else i can share to help with this. 让我知道我还有什么可以分享的帮助。

Found the solution to my problem 找到了解决我问题的方法

In the AppDelegate for iOS.Shared remove the line [Register("AppDelegate")] 在iOS的AppDelegate中。删除行[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);
    }
}

In the AppDelegate for iOS.ClientThemeX add the line var systemFunctions_iOS = new iOS.Shared.iOS_Interfaces.SystemFunctions_iOS(); 在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);
    }
}

removing the AppDelegate register from the shared project makes sense, but i'm not sure why i needed to declare a new object with my interface to work. 从共享项目中删除AppDelegate寄存器很有意义,但是我不确定为什么我需要使用接口声明一个新对象才能正常工作。 This interface constructor, as in the question, is empty. 如问题所示,此接口构造函数为空。

If anyone could shed any light on this still it would be appreciated, or if there is any advice for a much simpler way of doing this. 如果有人可以对此有所了解,还是可以提出任何更简单的方法的建议,我们将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM