Tag Archives: Application

Caliburn Micro Part 1: Getting Started

In this blog series I’ll go through the steps needed to get up and running with Caliburn Micro, we’ll be using a wpf application as an example although caliburn micro also supports Silverlight, Windows Phone and Windows Store apps.

Step 1: Initial Setup

Start by creating a new wpf application in Visual Studio and grab the Caliburn Micro nugget package using the library package manager. Since Caliburn Micro takes care of creating all windows for you, delete MainWindow.xaml and remove the StartupUri attribute from App.xaml. After doing the previous App.xaml will look like this.

<Application x:Class="CaliburnMicroExample.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

Step 2: Creating a View Model

Since Caliburn Micro prefers a View-Model-First approach lets start by creating our first View Model. Add a new class to the project and call it ShellViewModel. Since we are just getting setup in this tutorial we won’t add any functionality to the VM. After you have created ShellViewModel it should look as below.

namespace CaliburnMicroExample
{
    public class ShellViewModel
    {
    }
}

Note that the names of View Models in Caliburn Micro must end with ViewModel although what you put before that is up to you.

Step 3: Creating a View

In order to display something to a user we’ll need a View to go along with the View Model, create a new wfp user control and call it ShellView. The xaml for the view you created should look as below.

<UserControl x:Class="CaliburnMicroExample.ShellView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"
             Width="100" Height="100">
    <Grid>
            
    </Grid>
</UserControl>

Note that Views in Caliburn Micro must end with View and have the same name as the View Model, so having a View Model called MyViewModel would mean that the associating View is called MyView. This is so that Caliburn Micro can automatically bind them together.

Step 4: The Bootstrapper

Finally we need a Bootstrapper which will configure the Caliburn Micro framework and tell it what to do. Bootstrappers can do quite a lot of things beyond the scope of this tutorial so for now we will just configure one that initialises the framework and displays the first View. So create a new class and call it AppBootstrapper, then add code to make it look like the one below.

using Caliburn.Micro;

namespace CaliburnMicroExample
{
    public class AppBootstrapper : BootstrapperBase
    {
        public AppBootstrapper()
        {
            Initialize();
        }

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

The last thing to do is to tell the application to use the bootstrapper on startup, to do this we need to edit App.xaml as shown below.

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

That’s all there is to it, if you run the application now you should see a small square window. You are now ready to take advantage of all the cool things that Caliburn Micro has to offer.

In the next part of this blog series I will go through how databinding and events work in Caliburn Micro.