This is the first part of post series for basics of using Caliburn micro for MVVM framework in WPF. In this blog post we will see the benefits of using a MVVM framework and how it can ease your life when working with MVVM design specially with WPF. For the series I’ve chosen Caliburn Micro because this is a lightweight, powerful, easy to use and highly recommended framework with a very good technical documentation. Second we’ll talk about the SimpleInjector for IOC (Inversion of control) because this is reported as fastest and easy to use IOC container framework. IOC container is required with Caliburn micro in the bootstrapper(Entry point) to register all the view model types and other types so that they can be resolved at run times. The IOC keeps our application fully decoupled and this is what MVVM is for. You’ll see the true MVVM taste here with fully decoupled views and view models in upcoming posts.
So Let’s get started with the first step writing the Bootstrapper for the WPF application:
Application Bootstrapper – The bootstrapper is required to register and configure all the runtime and startup components. This will be your entry point of your WPF application. And rather than directly adding the referencing of your startup window you’ll be registering the bootstrapper in App.XAML
Writing the bootstrapper -
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Caliburn.Micro;
- using DataGridSample;
- using SimpleInjector;
- namespace CaliburnMicroMvvmWpfDemo
- internal class AppBootstrapper : Bootstrapper<MainWindowView>
- /// <summary>
- /// The global container.
- /// </summary>
- public static readonly Container ContainerInstance = new Container();
- protected override void Configure()
- ContainerInstance.Register<IWindowManager, WindowManager>();
- ContainerInstance.RegisterSingle<IEventAggregator, EventAggregator>();
- protected override IEnumerable<object> GetAllInstances(Type service)
- return ContainerInstance.GetAllInstances(service);
- protected override object GetInstance(System.Type service, string key)
- return ContainerInstance.GetInstance(service);
- protected override void BuildUp(object instance)
In the above code in order to create a bootstrapper we just need to tell the inbuilt Bootstrapper<> the type of startup window view model and inherit the class in our AppBootstrapper class. Now we need to override few inbuilt method of Bootstrapper<> in order to complete the configuration. Register the interfaces of IWindowManager and IEventAggregator (Optional and for later use) in the configure method. Just use the above code to write your initial bootstrapper though you need to add more lines of code in the Configure method to register your all other view model types later.
Updating the App.XAML
To add the entry of bootstrapper change the App.xaml file to look something like below.
- <Application x:Class="CaliburnMicroMvvmWpfDemo.App"
- xmlns:Boot ="clr-namespace:CaliburnMicroMvvmWpfDemo">
- <Boot:AppBootstrapper x:Key = "bootStrapper" />
Once you changed the above setting your application is ready to launch. You have successfully created the entry point for the application. Now to have your Global even handler Caliburn Micro provide overrides for internal exception handlers which you had in App.xaml.cs. Below events could be added in the AppBootstrapper class.
- protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
- // do custom code here which you want to run on startup
- base.OnStartup(sender, e);
- protected override void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
- // Write you custom code for handling Global unhandled excpetion of Dispatcher or UI thread.
- base.OnUnhandledException(sender, e);
Here in this post I tried to cover the starting step of adding the bootstrapper for caliburn micro. Also if you can see we have create our MainWindowViewModel class first but haven’t wrote anything in it neither we have any view here. In the post we’ll see creating main window view model and basic binding and other MVVM stuff.
Hope you enjoyed it. Please leave a comment or suggestion.