I'm trying to use Caliburn Micro's built-in EventAggregator
in my WPF project, based on this documentation. But the documentation (as I almost always find it with Caliburn Micro) seems incomplete, i.e. the sample code doesn't contain all implementation requirements.
In particular, I wasn't using an IoC container in my project, and I'm sure I'm missing or misusing some part of its configuration, and that is causing the issue.
The problem:
NullReferenceException
in the DisplayRootViewFor
method on startup.NullReferenceException
is generally, but I'd like to know how to solve this issue specifically connected to Caliburn Micro's app bootstrapper configuration (and to help other users who could encounter the same problem while trying to do the same thing).Notes:
NullReferenceException
is connected to the GetInstance
override method, as if I comment it out, I get the "No parameterless constructor defined for this object" exception.Could somebody explain to me why does Caliburn.Micro throw this exception, and how I'm supposed to inject the EventAggregator
to my main viewmodel? (Because trying to pass it as a parameter in DisplayRootViewFor
didn't seem to work.)
My app bootstrapper looks like the following – based on the combination of The Event Aggregator and the Simple IoC Container documentation pages:
public class AppBootstrapper : BootstrapperBase
{
private readonly SimpleContainer _container =
new SimpleContainer();
public AppBootstrapper()
{
Initialize();
}
protected override void Configure()
{
_container.Singleton<IEventAggregator, EventAggregator>();
}
protected override object GetInstance(Type serviceType, string key)
{
return _container.GetInstance(serviceType, key);
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return _container.GetAllInstances(serviceType);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<MainViewModel>();
}
}
And my MainViewModel
's constructor looks like the following – set up for injecting the EventAggregator
:
public MainViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
Use the RegisterPerRequest
to register the view model type itself. Try this:
public class HelloBootstrapper : BootstrapperBase
{
private readonly SimpleContainer _container = new SimpleContainer();
public HelloBootstrapper()
{
Initialize();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
base.OnStartup(sender, e);
DisplayRootViewFor<MainViewModel>();
}
protected override void Configure()
{
_container.Singleton<IWindowManager, WindowManager>();
_container.Singleton<IEventAggregator, EventAggregator>();
_container.RegisterPerRequest(typeof(MainViewModel), null, typeof(MainViewModel));
}
protected override object GetInstance(Type serviceType, string key)
{
return _container.GetInstance(serviceType, key);
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return _container.GetAllInstances(serviceType);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
}
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