IMvxAndroidCurrentTopActivity是单身吗?

Dan*_*rth 2 c# xamarin.android mvvmcross xamarin

IMvxAndroidCurrentTopActivity 可用于获取MvvmCross Android应用程序中的当前顶级活动.

问题是:一旦顶级活动发生变化,MvvmCross是否会创建此接口的新实例,或者它是否会重复使用同一个实例并只更改Activity属性.

背景:我想将该接口作为注册为singleton的类中的构造函数依赖项.
它会起作用吗?

Stu*_*art 9

在默认的MvvmCross设置中,该接口实现为单例 - 只要您的应用程序在内存中,保证返回相同的实例

InitializePlatformServiceshttps://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Droid/Platform/MvxAndroidSetup.cs#L76

    protected override void InitializePlatformServices()
    {
        var lifetimeMonitor = new MvxAndroidLifetimeMonitor();
        Mvx.RegisterSingleton<IMvxAndroidActivityLifetimeListener>(lifetimeMonitor);
        Mvx.RegisterSingleton<IMvxAndroidCurrentTopActivity>(lifetimeMonitor);
        Mvx.RegisterSingleton<IMvxLifetime>(lifetimeMonitor);

        Mvx.RegisterSingleton<IMvxAndroidGlobals>(this);

        var intentResultRouter = new MvxIntentResultSink();
        Mvx.RegisterSingleton<IMvxIntentResultSink>(intentResultRouter);
        Mvx.RegisterSingleton<IMvxIntentResultSource>(intentResultRouter);

        var viewModelTemporaryCache = new MvxSingleViewModelCache();
        Mvx.RegisterSingleton<IMvxSingleViewModelCache>(viewModelTemporaryCache);
    }
Run Code Online (Sandbox Code Playgroud)

这个单例性质实际上是这个接口定义的一部分 - 所以如果你要覆盖android设置,那么你应该把这个注册保持为单身.


在一般情况下,我担心目前只有在MvvmCross中告诉接口或对象是注册为单例还是动态创建按需对象的方式 - 查看源代码

在将来,这可能通过XML注释或通过某种命名约定来实现,但我不认为这些是当前计划的,并且这些技术都不会被编译时检查.