Windows Store App with Caliburn.Micro - Getting Started (Updated)

posted on 20 Feb 2013 | Windows 8 App

Note: This is an update post for Windows Store App with Caliburn.Micro - Getting Started

Back in December I blogged about getting started with Caliburn.Micro, not long after I created the post a new version (v1.4.1) was released.

The changes in this release break my previous blog post so I'm updating here.

Note: The original post still applies, the change is only to the setup of the App

App Setup

In the previous post when we configured the container, all we needed to do was Register the WinRT Services.

protected override void Configure()
{
    container = new WinRTContainer();
    container.RegisterWinRTServices();
}

However, the default container no longer auto creates concrete types, which causes View Models to not auto create. To fix this we do one of two things.

Manually Register Types

In the Configure method, we can manually register the View Model like so

protected override void Configure()
{
    container = new WinRTContainer();
    container.RegisterWinRTServices();
    container.PerRequest<MainViewModel>();
}

Running the app, the View Model should now be created when you navigate to the View. The downside to this approach is that you need to do this for every view model which can be tedious.

Automate the Registration

Note: This approach came from the samples.

Instead of manually registering, we can automate it by over-riding the GetInstance method and manually registering the type if it doesn't exist.

private static bool IsConcrete(Type service)
{
    var serviceInfo = service.GetTypeInfo();
    return !serviceInfo.IsAbstract && !serviceInfo.IsInterface;
}

protected override object GetInstance(Type service, string key)
{
    var obj = container.GetInstance(service, key);

    // mimic previous behaviour of WinRT SimpleContainer
    if (obj == null && IsConcrete(service))
    {
        container.RegisterPerRequest(service, key, service);
        obj = container.GetInstance(service, key);
    }

    return obj;
}

In this example we attempt to get the instance, if it doesn't exist, but it's a concrete type, then we register it and then Get/Return it.

And that's it :)

comments powered by Disqus