Setting up dependency injection with Caliburn Micro & Ninject
Asked Answered
N

1

6

I'm trying to set up dependency injection in a new WPF project using the framework Caliburn Micro and Ninject. Unfortunately I'm not succeeding :( There are a few examples on the internet which implement a generic Bootstrap but for me the generic Bootstrap class is not available and since all these examples are at least 3 years old I guess they are deprecated...

What I've tried is the following:

public class CbmBootstrapper : BootstrapperBase
{
    private IKernel kernel;

    protected override void Configure()
    {
        this.kernel = new StandardKernel();

        this.kernel.Bind<IAppViewModel>().To<AppViewModel>();
    }
}

And in the App.xaml

<Application x:Class="CBMExample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:local="clr-namespace:CBMExample"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary>
            <local:CbmBootstrapper x:Key="bootstrapper" />
        </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

I am very new to WPF and Ninject. Can you tell me what I have to change, so that at startup of the application, the View (AppView) with the corresponding ViewModel (AppViewModel) gets loaded?

Do you know of any up-to-date tutorial on using & setting up Ninject with Caliburn Micro?

Nicety answered 16/3, 2015 at 11:22 Comment(0)
T
4

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...

Tempa answered 16/3, 2015 at 12:1 Comment(2)
Hi Simon thanks for your answer! This seems to make sense :D I added the override for OnStartup and GetInstance to the CbmBootstrapper but unfortunately, my view still doesn't get started... do I have to add something to the App.xaml.cs as well so that it recognizes my bootstrapper and uses this? My breakpoint at OnStartup of the CbmBootstrapper isn't getting hit... Thanks in advanceNicety
Hi @xeraphim, see my edit. The call to Initialize is what really gets the ball rolling with Caliburn.Micro, rather essential information I forgot...Tempa

© 2022 - 2024 — McMap. All rights reserved.