MvvmCross初始化

4
在问题“如何从内容提供程序和活动中使用MvvmCross”中,我想知道如何初始化MvvmCross系统。
当时给出的答案是有效的,但随着MvvmCross的最新更新,我使用的函数(MvxAndroidSetupSingleton.GetOrCreateSetup())已被弃用。
我现在已经更改了我的初始化方式,并且目前似乎可以正常工作。但这是否正确和适当?我是否应该采取不同的方法来改进可移植性?
在Android平台特定的DLL中的Setup类:
public class Setup
   : MvxAndroidSetup
{
    public Setup(Context applicationContext)
        : base(applicationContext)
    {
    }

    protected override IMvxApplication CreateApp()
    {
        // Create logger class which can be used from now on
        var logger = new AndroidLogger();
        Mvx.RegisterSingleton(typeof(ILogger), logger);
        var app = new App();
        InitialisePlatformSpecificStuff();
        return app;
    }

    private void InitialisePlatformSpecificStuff()
    {
        // For instance register platform specific classes with IoC
    }
}

我的App类在可移植核心库中:

public class App
    : MvxApplication
{
    public App()
    {
    }

    public override void Initialize()
    {
        base.Initialize();
        AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
        InitialisePlugins();
        InitaliseServices();
        InitialiseStartNavigation();
    }

    private void InitaliseServices()
    {
        CreatableTypes().EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();
    }

    private void InitialiseStartNavigation()
    {
    }

    private void InitialisePlugins()
    {
        // initialise any plugins where are required at app startup
        // e.g. Cirrious.MvvmCross.Plugins.Visibility.PluginLoader.Instance.EnsureLoaded();
    }

    public static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
    {
        // Log exception info etc
    }
1个回答

3

我使用的函数(MvxAndroidSetupSingleton.GetOrCreateSetup())已被弃用。

MvvmCross初始化的更改是为了帮助用户避免“多个启动屏幕”问题-请参见https://github.com/slodge/MvvmCross/issues/274

这些变化的核心是:

所以您可以看到,此更改已删除以下行:
-  var setup = MvxAndroidSetupSingleton.GetOrCreateSetup(activity.ApplicationContext);
-  setup.EnsureInitialized(androidView.GetType());

并用以下内容替换它们:

+  var setupSingleton = MvxAndroidSetupSingleton.EnsureSingletonAvailable(activity.ApplicationContext);
+  setupSingleton.EnsureInitialized(); 

所以,您的更改需要反映出相同的代码。

哎呀,我忘记在OnCreate中包含我的设置代码了... 它就像你展示的那样。 - Krister Renaud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接