开发者

Injecting dependencies into an MVVMLight ViewModelLocator

开发者 https://www.devze.com 2023-03-21 10:09 出处:网络
I have a ViewModelLocator from MVVMLight containing my MainViewModel. I have another singleton class ResourceLogger which does something else.

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().InSin开发者_如何学运维gletonScope();

Is there some way to share the kernel between App.xaml.cs and ViewModelLocator?

How can I resolve this problem?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消