MvvmCross initialization

﹥>﹥吖頭↗ 提交于 2020-01-09 08:01:24

问题


In question Using MvvmCross from content providers and activities I wanted to know of how to initialize the MvvmCross system.

The answer given worked then, but with recent updates of MvvmCross the function which I used (MvxAndroidSetupSingleton.GetOrCreateSetup()) has been deprecated.

I have now changed my initialization and it seems to work so far, but is it correct and proper? Should I do things differently to improve portability?

Setup class, in platform specific DLL for Android:

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
    }
}

And my App class in the portable core library:

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:


the function which I used (MvxAndroidSetupSingleton.GetOrCreateSetup()) has been deprecated.

The changes to MvvmCross initialization were required to help users avoid 'multiple splashscreen' issues - see https://github.com/slodge/MvvmCross/issues/274.

The core of these changes was:

  • https://github.com/slodge/MvvmCross/commit/37f9dc76d3eca1fd2a7379597b73365c772e23a3

So you can see that this change removed the lines:

-  var setup = MvxAndroidSetupSingleton.GetOrCreateSetup(activity.ApplicationContext);
-  setup.EnsureInitialized(androidView.GetType());

and replaced them with:

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

So your changes will need to reflect this same code.



来源:https://stackoverflow.com/questions/17466140/mvvmcross-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!