Caliburn Micro Part 5: WinRT and Phone 8.1

Welcome to the next part of this blog series on Caliburn Micro, in the previous part we saw how the Event Aggregator can be used to pass messages between different View Models. In this next part we will see how Caliburn Micro can be used in a Windows RT or Windows Phone 8.1 application.

It should be noted that Visual Studio 13 or higher and Windows 8.1 are required to develop Windows Phone 8.1 and Window Store 8.1 apps.

In the following examples a Windows Phone 8.1 one app is used but the procedure should be similar if not the same for Windows Store apps.

Step 1: Getting Started

First off create a new Windows Phone 8.1 or a Windows Store project and grab the Caliburn Micro nugget package from the Library Package Manager in Visual Studio.

Step 2: Adding Caliburn Micro Support

If you have used Caliburn Micro in other applications you will probably have created a bootstrapper to set up all the Caliburn Micro related stuff and start your application. Windows Phone 8.1 and Windows Store apps work in a slightly different way so a bootstrapper is not required. If you look in the solution explorer there should be a file called App.xaml, we will start by editing this.

Edit the file so that it looks like the code below, note that x:Class is unique to the name of your application and so should be left as it is.

<caliburn:CaliburnApplication
    x:Class="YourAppName.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:caliburn="using:Caliburn.Micro">

</caliburn:CaliburnApplication>

Next we need to edit App.xaml.cs to add code that will setup and start our application. Change App.xaml.cs so that it looks as below.

public sealed partial class App : CaliburnApplication
{
    private WinRTContainer container;

    public App()
    {
        InitializeComponent();
    }

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

        container.RegisterWinRTServices();

        container.PerRequest<ShellViewModel>();
    }

    protected override void PrepareViewFirst(Frame rootFrame)
    {
        container.RegisterNavigationService(rootFrame);
    }

    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        DisplayRootViewFor<ShellViewModel>();
    }

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

    protected override IEnumerable<object> GetAllInstances(Type service)
    {
        return container.GetAllInstances(service);
    }

    protected override void BuildUp(object instance)
    {
        container.BuildUp(instance);
    }
}

Things to take note of here are that you need to add a new entry for every View Model that you create, so in the Configure function you would add a new container.PerRequest with the name of your View Model. Also of note is the DisplayRootViewFor in the OnLaunched function, this needs to be changed to whichever View Model you want displayed when your app starts up.

Step 3: Develop Your Application

That’s about all you need to know about setting up the application, if you need help on how the rest of Caliburn Micro works please see the previous blog posts in this series.

That’s it for this tutorial join us next time for a look at using the Window Manager.

7 thoughts on “Caliburn Micro Part 5: WinRT and Phone 8.1

    1. João Miranda

      Clayton, i always follow your Caliburn tutorials, they are really good!

      I am beggining to work with Caliburn.Micro on Windows Phone and when I try to extend the ‘CaliburnApplication’ on ‘App.xaml.cs’ it presents me with the error:

      “The type or namespace name ‘Application’ does not exist in the namespace ‘Caliburn.Micro’ (are you missing an assembly reference?)”.

      I made everything as you said in the tutorial, but when i try to make the first tag on App.Xaml like ” it doesn’t let me do it.

      Am i missing something?

      Reply
      1. João Miranda

        Update the error on ‘App.xaml.cs’ is:
        “Partial declarations of ‘UI.App’ must not specify different base classes”

        not the one above =P

      2. claytonone Post author

        It’s probably because they are inheriting from different base classes, they must both inhert CaliburnApplication. So you need this

        public sealed partial class App : CaliburnApplication

        and just copy the xml I posted in the tutorial but making sure “YourAppName.App” has been changed to the name of your application, so if my app was called CaliburnTutorial it would be CaliburnTutorial.App

  1. Pingback: Windows Phone 8.1 – Some useful links for developing Windows “Universal Apps” | Enzo Contini Blog

  2. Pingback: MVVM frameworks for Windows RealTime (Universal Apps): which one to choose? | Enzo Contini Blog

  3. Pingback: MVVM frameworks for Windows RealTime (Universal Apps): which one to choose? | Enzo Contini Blog

Leave a comment