将依赖项注入 MVVMLight ViewModelLocator
我有一个来自 MVVMLight 的 ViewModelLocator
,其中包含我的 MainViewModel
。
我有另一个单例类 ResourceLogger
,它执行其他操作。
ResourceLogger
是在 App.xaml.cs
中使用以下方式创建的:
var resourceLogger = kernel.Get<ResourceLogger>();
MainViewModel
依赖于 ResourceLogger
。
我在将 ResourceLogger
注入 MainViewModel
时遇到问题,如下所示:
[Inject]
public MainViewModel(ResourceLogger resourceLogger) { ... }
问题是它创建了两个 ResourceLogger
类。
NinjectModule 如下所示:
Bind<ResourceLogger>().ToSelf().InSingletonScope();
Bind<MainViewModel>().ToSelf().InSingletonScope();
是否有某种方法可以在 App.xaml.cs 和 ViewModelLocator 之间共享内核?
我该如何解决这个问题?
I have a ViewModelLocator
from MVVMLight containing my MainViewModel
.
I have another singleton class ResourceLogger
which does something else.
ResourceLogger
is created in App.xaml.cs
using:
var resourceLogger = kernel.Get<ResourceLogger>();
MainViewModel
depends on ResourceLogger
.
I am having problems injecting ResourceLogger
into the MainViewModel
as follows:
[Inject]
public MainViewModel(ResourceLogger resourceLogger) { ... }
The problem is that it creates two ResourceLogger
classes.
The NinjectModule looks as follows:
Bind<ResourceLogger>().ToSelf().InSingletonScope();
Bind<MainViewModel>().ToSelf().InSingletonScope();
Is there some way to share the kernel between App.xaml.cs and ViewModelLocator?
How can I resolve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是为内核创建公共静态类/变量,然后将其用于所有注册和对 Get() 的调用。
顺便说一句,您应该尽您所能在整个系统中只调用一次 kernel.Get() (或至少尽可能少的调用)。
Easiest way would be to have create public static class/variable for the kernel, then use it for all registration and calls to Get().
As an aside, you should do what you can to only have one call to kernel.Get() (or at least as few calls as possible) in your whole system.