Over a million developers have joined DZone.

Real World Windows Phone Dev Part 3: Caliburn.Micro and Application Infrastructure

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

This series is going to walk through building a polished, functioning Windows Phone app from start to finish. The app is called Realworld Stocks and the full source code will be available on CodePlex as the series progresses. I’ll be using Mercurial to encourage forking and maybe even pull requests from developers who want to contribute their own real-world solutions.

Make sure you DOWNLOAD the Windows Phone 7.5 SDK

View the Series Introduction and Outline


Many Windows Phone Applications use a lightweight MVVM framework for basic infrastructure services. Realworld Stocks is going to use Caliburn.Micro. I have had great success with Caliburn.Micro in my previous Windows Phone and Silverlight apps. CM was created by Rob Eisenberg and is under active development and has a great community and following.

This article is going to walk through a lot of Caliburn.Micro setup, but for the full documentation please see the official project documentation.

Installing Caliburn.Micro from NuGet

If you’ve been following along then you already have NuGet installed. If not, please see Part 2: File –> New Project.

Caliburn.Micro needs to be installed into both the RealWorldStocks.Client.Core and the RealWorldStocks.Client.UI projects.

From the Package Manager Console

To get Caliburn.Micro you can open up the Package Manager console and type the following, but make sure you select the right Default Project. You must install CM into both .Core and .UI projects!


From the Manage NuGet Packages UI

Alternatively you can right-click on both Projects from the Solution Explorer and select “Manage NuGet Packages…”, then click the Online pane and search for Caliburn.Micro. You must install CM into both .Core and .UI projects!



Finishing Caliburn Setup

Once Caliburn.Micro finishes installing it should open up a web page with instructions to finish the configuration.

  1. Follow the instructions that opened up in Visual Studio
  2. Make sure your App.xaml and App.xaml.cs are cleared out from the instructions above!
  3. Delete MainPage.xaml
  4. In RealworldStocks.Client.UI, create a new folder called Framework
  5. Delete AppBootstrapper.cs
  6. Create a new AppBootstrapper.cs file in the Framework folder
  7. Paste the following code into AppBoostrapper.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using Caliburn.Micro;
using Microsoft.Phone.Controls;
using RealWorldStocks.Client.UI.ViewModels.Home;
namespace RealWorldStocks.Client.UI.Framework
   public class AppBootstrapper : PhoneBootstrapper
        PhoneContainer _container;
        protected override void Configure()
            _container = new PhoneContainer(RootFrame);
        protected override void OnUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
            if (Debugger.IsAttached)
                e.Handled = true;
                MessageBox.Show("An unexpected error occured, sorry about the troubles.", "Oops...", MessageBoxButton.OK);
                e.Handled = true;
            base.OnUnhandledException(sender, e);
        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)

8. Since we moved the AppBoostrapper into the RealWorldStocks.Client.UI.Framework namespace we need to update our App.xaml. Paste the following into your App.xaml

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                <ResourceDictionary Source="Assets/Constants.xaml"/>
                <ResourceDictionary Source="Assets/Styles.xaml"/>
                <ResourceDictionary Source="Assets/Converters.xaml"/>
            <Framework:AppBootstrapper x:Key="bootstrapper" />

Working around a small NuGet bug

At the time of this writing there appears to be a bug in the NuGet package which thinks my project is a Silverlight project (instead of Windows Phone).

If the NuGet package created a ShellViewModel.cs in your project, then you will need to take the following steps:

  1. Delete ShellViewModel.cs
  2. Delete IShell.cs
  3. Delete ShellView.xaml

Application Infrastructure

imageThe final pieces of our infrastructure include the following:

Value Converters

I have a set of IValueConverters that I have used on all of my Windows Phone projects. They can be viewed from the CodePlex source code.


Assets are a set of ResourceDictionaries that contain the resources used throughout the application. They can be viewed from the CodePlex source code.


Defines variables like “AppName” so it can be used on pages
<TextBlock Text=”{StaticResource AppName}” />


Defines the set of converters used in our project


Defines a set of reusable styles



ViewModels define the logic for all of our views. They take advantage of XAML’s great binding mechanisms and Caliburn’s ActionMessages to control how our views behave. Later in the series we will deep dive into Views and ViewModels.

To get started, make sure you have a ViewModels folder, then right-click and select New Item…, add a Class file called HomeViewModel.cs

public class HomeViewModel : Screen
    public HomeViewModel()
        DisplayName = "Home";


Views are your .xaml Pages and UserControls. Later in the series we will deep dive into Views and ViewModels.

To get started, make sure you have a Views folder, then right-click and select New Item…, add a Windows Phone Portrait Page called HomeView.xaml


Lastly, you need to update the WMAppManifest to tell it what page should load when the app is launched.

Open WMAppManifest.xml and set the NavigationPage to your primary page

<DefaultTask Name="_default" NavigationPage="Views/HomeView.xaml" />

Run it!

Now we’re getting somewhere. We have the majority of our application infrastructure in place and have our first View and ViewModel. If you press F5 now it should launch right into the HomeView!


If the app exits immediately after launching check the following:

  1. WPAppManifest NavigationPage points at a .xaml file that actually exists
  2. Make sure you App.xaml.cs has nothing but InitializeComponent() and App.xaml only has the Bootstrapper in it. See the Caliburn.Micro documentation for more
  3. Check the namespaces in your ViewModels and Views and make sure ViewModels and Views namespaces match up
  4. For further Caliburn troubleshooting check the Caliburn.Micro documentation

Source:  http://www.matthidinger.com/archive/2011/10/16/RealWorldWPDev-Part-3-Caliburn-Micro-and-Application-Infrastructure.aspx

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}