NullReferenceException in Caliburn.Micro DisplayRootViewFor method when trying to use EventAggregator

I am trying to use built-in EventAggregator

Caliburn Micro in my WPF project based on this documentation . But the documentation (as I almost always find it with Caliburn Micro) seems incomplete, meaning the sample code does not contain all the implementation requirements.

In particular, I have not used an IoC container in my project and I am sure that I am missing or misusing part of its configuration and that is causing the problem.

Problem:

  • I am getting NullReferenceException

    in the method DisplayRootViewFor

    on startup.
  • I obviously know what it is NullReferenceException

    , but I would like to know how to solve this problem specifically related to the Caliburn Micro boot buffer configuration (and help other users who may face the same problem when trying to do the same) ...

Notes:

  • It is clear that I am not clear on how the application bootstrap works as I have always found the Caliburn Micro documentation to be very difficult to compare (versus MSDN) and I have not used an IoC container before.
  • NullReferenceException

    connected to an override method GetInstance

    , as if I had commented it out, I get a "No parameters, no constructor for this object" exception.
  • I already tried the solution in this question , but it didn't seem to make any difference.

Can anyone explain to me why Caliburn.Micro is throwing this exception and how should I embed EventAggregator

in my main viewmodel?
(because trying to pass it as a parameter in DisplayRootViewFor

doesn't seem to work.)


My app bootstrapper looks like this - based on a combination of Event Aggregator and Simple IoC Containers :

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 constructor MainViewModel

looks like this, set up for input EventAggregator

:

public MainViewModel(IEventAggregator eventAggregator)
{
    _eventAggregator = eventAggregator;
}

      

+3


source to share


1 answer


Use RegisterPerRequest

to register the view model type itself. Try the following:



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);
    }
}

      

+4


source







All Articles