是IMvxAndroidCurrentTopActivity单身? [英] Is IMvxAndroidCurrentTopActivity a singleton?
问题描述
IMvxAndroidCurrentTopActivity
可用于获取一个MvvmCross Android应用目前最顶级的活动。
IMvxAndroidCurrentTopActivity
can be used to get the current top activity in a MvvmCross Android application.
现在的问题是:请问MvvmCross尽快创建这个接口的一个新实例作为顶级活动的变化或是否重用的相同的实例,只需更改活动
属性
The question is: Will MvvmCross create a new instance of this interface as soon as the top activity changes or does it reuse the same instance and just change the Activity
property.
背景:?我想借这个接口作为注册为单身一类的构造函数依赖结果
就它的工作
Background: I would like to take that interface as a constructor dependency in a class that is registered as singleton.
Will it work?
推荐答案
在默认MvvmCross设置该接口被作为一个单独实现的 - 这样可以保证,只要返回相同的实例作为您的应用程序在内存
In the default MvvmCross setup that interface is implemented as a singleton - so is guaranteed to return the same instance as long as your app is in memory
请参阅 InitializePlatformServices
中的https://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);
}
这单自然是真的这个接口的定义的一部分 - 所以,如果你是覆盖了Android的设置,那么你应该保持这个注册为单身。
This singleton nature is really part of the definition of this interface - so if you were to override the android setup, then you should really keep this registration as a singleton.
在一般的意义,我恐怕只有目前在MvvmCross告诉接口或对象是否被注册为单身或作为一个动态创建按需对象的方式 - 以查看源
At a general level, I'm afraid there is only way to currently tell in MvvmCross whether an interface or object is registered as a singleton or as a dynamic creation-on-demand object - to look at the source
- e.g. https://github.com/slodge/MvvmCross/search?q=IMvxAndroidCurrentTopActivity&ref=cmdform.
在未来,这也可能会被通过XML注释,或者通过某种命名约定的实现,但我不不敢相信这些都是目前并还计划没有这些技术将是编译期进行检查。
In the future, this could possibly be achieved via XML comments or via some kind of naming convention, but I don't believe these are planned currently and also neither of those techniques would be compile time checked.
这篇关于是IMvxAndroidCurrentTopActivity单身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!