[英]How solve circular reference in Caliburn.Micro
I am working with Caliburn.Micro v2.0.1 on a Windows 8.1 Unversal (WinRT) project. 我正在Windows 8.1 Unversal(WinRT)项目上使用Caliburn.Micro v2.0.1。
I followed the Caliburn.Micro Working with WinRT example. 我以Caliburn.Micro使用WinRT为例。
My code looks as follows: 我的代码如下所示:
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
Initialize();
DisplayRootViewFor<LoginViewModel>();
}
protected override void PrepareViewFirst(Frame rootFrame)
{
_container.RegisterNavigationService(rootFrame);
}
public LoginViewModel(INavigationService navigationService, ...)
{
...
}
The OnLaunched
is called first. OnLaunched
调用OnLaunched
。
Initialize()
Configures the WinRT container. Initialize()
配置WinRT容器。
DisplayRootViewFor<LoginViewModel>
invokes an instance of the LoginViewModel
and results in a Null exception because NavigationService has not yet been registered by PrepareViewFirst(Frame)
DisplayRootViewFor<LoginViewModel>
调用的实例LoginViewModel
和因为的NavigationService尚未通过注册导致空异常PrepareViewFirst(Frame)
PrepareViewFirst(Frame)
is not yet called, having a dependency on the RootFrame
that should be configured by OnLaunched
PrepareViewFirst(Frame)
尚未调用,它依赖于RootFrame
配置的OnLaunched
Thus LoginViewModel
is dependent on RegisterNavigationService
and RegisterNavigationService
is dependent on DisplayRootViewFor<LoginViewModel>()
which is dependent on LoginViewModel
因此,
LoginViewModel
依赖于RegisterNavigationService
而RegisterNavigationService
依赖于DisplayRootViewFor<LoginViewModel>()
,后者依赖于LoginViewModel
Is there any way to overcome this circular reference issue? 有什么办法可以克服这个循环参考问题?
Register your services in the container before resolving the Views
- this way all dependencies are available in the particular Dependency Injection container and you can use ServiceLocator
to find them. 在解决
Views
之前,先在容器中注册服务-这样,所有依赖关系都可以在特定的依赖注入容器中使用,并且您可以使用ServiceLocator
来查找它们。
Typically I've always done this in the OnStartup()
method of App.xaml.cs
. 通常,我总是在
App.xaml.cs
的OnStartup()
方法中完成此操作。
You should register/configure your container at the composition root , the earliest access point of your application. 您应该在组合根目录 (应用程序的最早访问点)上注册/配置容器。
This point depends on what kind of Application you have: 这点取决于您拥有哪种应用程序:
etc. 等等
Check the Windows 7 lifecycle on http://msdn.microsoft.com/en-us/magazine/hh148153.aspx 在http://msdn.microsoft.com/zh-cn/magazine/hh148153.aspx上检查Windows 7生命周期
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.