Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

xamarin - MvvmCross initialization

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
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...