Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caliburn.Micro DisplayRootViewFor throws NullReferenceException

I have the following code in my Bootstrapper:

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 handled in user code

I checked the source code of CM and used the same code to test 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>();
    }

Strangely, there was no Exception at those lines. Both view and viewmodel have reference, and the constructor of InitialView gets called, but when it reaches and calls DisplayRootViewFor, it still throws an exception.

What should I change?

like image 987
Nestor Avatar asked Mar 01 '16 10:03

Nestor


1 Answers

My container was missing a critical component:

container.Singleton<IWindowManager, WindowManager>();
like image 112
Nestor Avatar answered Oct 04 '22 02:10

Nestor