开发者

Injecting dependencies into an MVVMLight 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().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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜