Setting up dependency injection with Caliburn Micro & Ninject Setting up dependency injection with Caliburn Micro & Ninject wpf wpf

Setting up dependency injection with Caliburn Micro & Ninject


You will need to override OnStartup as well to have your root view / viewmodel shown:

protected override void OnStartup(object sender, System.Windows.StartupEventArgs e){    DisplayRootViewFor<IAppViewModel>();}

This extra call replaced the previous, generic bootstrapper and allows you to choose the root view for your application at runtime.

You'll also need to override GetInstance to have Caliburn hook into Ninject:

protected override object GetInstance(Type serviceType, string key){    return container.Get(serviceType);}

This is called by Caliburn.Micro whenever it needs to construct something, so it's your one-stop-shop for injecting Ninject (other IoC containers are available!) into the process.

As for an up-to-date tutorial; there aren't so many around since Caliburn.Micro went to version 2, however their official documentation is generally pretty useful.

EDIT: One more thing you have to do! Make sure that your bootstrapper constructor calls Initialize:

public CbmBootstrapper (){               Initialize();}

This will kick Caliburn.Micro into action...