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?
My container was missing a critical component:
container.Singleton<IWindowManager, WindowManager>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With