Caliburn.Micro DisplayRootViewFor throws a NullReferenceException

My Bootstrapper has the following code:

private SimpleContainer container; protected override void Configure() { container = new SimpleContainer(); container.Singleton<IEventAggregator, EventAggregator>(); container.PerRequest<InitialViewModel>(); } protected override object GetInstance(Type service, string key) { return container.GetInstance(service, key); } protected override IEnumerable<object> GetAllInstances(Type service) { return container.GetAllInstances(service); } protected override void BuildUp(object instance) { container.BuildUp(instance); } 

In the OnStartup method, I call the DisplayRooViewFor method:

 protected override void OnStartup(object sender, StartupEventArgs e) { DisplayRootViewFor<InitialViewModel>(); } 

This is the InitialViewModel:

  private IEventAggregator eventAggregator; public InitialViewModel(IEventAggregator ea) { eventAggregator = ea; } 

Unfortunately, it throws a NullReferenceException:

An exception of type 'System.NullReferenceException' occurred in Caliburn.Micro.Platform.dll, but was not processed in user code

I checked the CM source code and used the same code to verify it:

  protected override void OnStartup(object sender, StartupEventArgs e) { var viewModel = IoC.GetInstance(typeof(InitialViewModel), null); var view = ViewLocator.LocateForModel(viewModel, null, null); ViewModelBinder.Bind(viewModel, view, null); var activator = viewModel as IActivate; if (activator != null) activator.Activate(); DisplayRootViewFor<InitialViewModel>(); } 

Strange, there was no exception to these lines. Both views and the viewmodel have a link, and the InitialView constructor is called, but when it reaches and calls DisplayRootViewFor , it throws an exception anyway.

What should i change?

+5
source share
3 answers

My container was missing a critical component:

 container.Singleton<IWindowManager, WindowManager>(); 
+8
source

You mix between SimpleContainer and MEF injection. You should use only one of them.

MEF : If your InitialViewModel needs to use MEF to install the constructor, you need to create a Bootstrapper to handle it, for example, in post . Remember to export your InitialViewModel and remove the SimpleContainer code.

SimpleContainer Or you remove MEF (just by removing ImportingConstructor-Attribute), SimpleContainer will take Job.

Your InitialViewModel should inherit the Caliburn.Micro Screen class, if attached to the main window.

0
source

Initialize (); The method should be called in your Bootstrapper CTOR.

0
source

Source: https://habr.com/ru/post/1244206/


All Articles