I am working with Caliburn.Micro v2.0.1 on a Windows 8.1 Unversal (WinRT) project.
I followed the Caliburn.Micro Working with WinRT example.
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.
Initialize()
Configures the WinRT container.
DisplayRootViewFor<LoginViewModel>
invokes an instance of the LoginViewModel
and results in a Null exception because NavigationService has not yet been registered by PrepareViewFirst(Frame)
PrepareViewFirst(Frame)
is not yet called, having a dependency on the RootFrame
that should be configured by OnLaunched
Thus LoginViewModel
is dependent on RegisterNavigationService
and RegisterNavigationService
is dependent on DisplayRootViewFor<LoginViewModel>()
which is dependent on 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.
Typically I've always done this in the OnStartup()
method of App.xaml.cs
.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.