Like most WPF/SL/WP developers, we use the MVVM pattern. Many frameworks help us to implement MVVM such as MVVM Light, Prism, ... My favorite is Caliburn.Micro. It's very powerful and simple. I use it for many projects on WPF / Windows Phone and Silverlight. Since a couple of week, a fork of Caliburn.Micro is now available on codeplex: http://caliburnmicro.codeplex.com/SourceControl/network/forks/marker_metro/CaliburnMicroWinRT. A great work has been made to respect the spirit of Caliburn.Micro.
As you know, Caliburn.Micro is very powerful with a DI/IOC container. As default, Caliburn.Micro for WinRT has its own IOC: WinRTContainer.
The purpose of this post is to show you how to mix Caliburn.Micro and Autofac.
Autofac is not available for WinRT or .NET 4.5 (like Unity 3.0) but a version with the support of Portable Libraries exists in beta. http://code.google.com/p/autofac/downloads/list
Caliburn.Micro for WinRT doesn't use a bootstrapper like wpf / sl but exetends Windows.UI.Xaml.Application with CaliburnApplication.
So you have just to replace the content of you App.xaml.cs with this code:
sealed partial class App : CaliburnApplication
{
private IContainer _container;
protected override Type GetDefaultViewModel()
{
return typeof(MainViewModel);
}
protected override void Configure()
{
// register Autofac container
var builder = new ContainerBuilder();
// register all caliburn services
builder.RegisterType<EventAggregator>().As<IEventAggregator>().SingleInstance();
builder.RegisterInstance(new FrameAdapter(RootFrame)).As<INavigationService>().SingleInstance();
// register all classes of your assemblies (such as view models)
builder.RegisterAssemblyTypes(AssemblySource.Instance.ToArray())
.AsSelf()
.InstancePerDependency();
_container = builder.Build();
}
protected override object GetInstance(Type service, string key)
{
object instance;
if (string.IsNullOrEmpty(key))
{
if (_container.TryResolve(service, out instance))
return instance;
}
else
{
if (_container.TryResolveNamed(key, service, out instance))
return instance;
}
throw new Exception(string.Format("Could not locate any instances of service {0}.", key jQuery15206574195942375809_1344857277230 service.Name));
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
IEnumerable<object> result = _container.Resolve(typeof(IEnumerable<>).MakeGenericType(service)) as IEnumerable<object>;
return result;
}
protected override void BuildUp(object instance)
{
_container.InjectProperties(instance);
}
}
Then, replace the declaration in the App.xaml file
<Micro:CaliburnApplication
x:Class="Mahalo.Travian.Windows8.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Micro="using:Caliburn.Micro">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/StandardStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Application-specific resources -->
<x:String x:Key="AppName">Mahalo.Travian.Windows8</x:String>
</ResourceDictionary>
</Application.Resources>
</Micro:CaliburnApplication>
And that's it ! You can now use Caliburn.Micro & Autofac into your Windows 8 app ! In a further post, we will show how to use the navigation, event aggregator, ...
Enjoy !
I will explain in this post how to use the IOC container of the Phone7.Fx with the MVVM pattern in a simple Windows Phone app. If you need more details on IOC and MVVM, take a look of the definition in Wikipedia: MVVM - IOC.
The sample (you can find the download link below) contains a View, a view model and service.

The view
The view is a simple XAML page located in the Views directory (it's not mandatory, but it's cleaner in Visual Studio). The first step is to add a behavior called ViewModelBehavior in the XAML code:
<i:Interaction.Behaviors>
<Mvvm:ViewModelBehavior/>
</i:Interaction.Behaviors>
The behavior is used to automatically find the associated ViewModel and to bind it to the datacontext of the View.
The next step is to add an IMainView which implement IView, the in the code behind, the MainView class implements the IMainView interface.
public interface IMainView:IView
{
}
public partial class MainView : IMainView
{
// Constructor
public MainView()
{
InitializeComponent();
}
}
Register a service
In our case, the HelloService provide some data. The service implements the IHelloService.
public class HelloService:IHelloService
{
public string SayHello()
{
return "Hello Phone7.Fx !";
}
}
With an IOC container, it's not our work to build and to manage the lifetime of the HelloService instance.
In the Application_Launching method in the App.xaml.cs file, we register the mapping between the IHelloService and the HelloService. In this, case, when we will need an implementation of IHelloService, the IOC container will resolve the mapping and provide an instance of HelloService.
private void Application_Launching(object sender, LaunchingEventArgs e)
{
Container.Current.RegisterType<IHelloService, HelloService>();
}
The ViewModel
The viewmodel is automatically bind to the view datacontext with the ViewModelBehavior. To really link them, we have to add some attributs on the View Model class.
The attribut ViewModel links the ViewModel to the View. In our case, the viewmodel needs an implementation of IHelloService. Thanks to the IOC container, the MainViewModel object is automatically instanciated and an instance of IHelloService is provided.
Now, it's possible to call the method SayHello of IHelloService and to set the value of the Message property.
Using the IOC on a viewmodel is very usefull because it's, now, very easy to mock and to implement unit tests on the view model.
[ViewModel(typeof(MainView))]
public class MainViewModel : ViewModelBase<IMainView>
{
private readonly IHelloService _helloService;
[Injection]
public MainViewModel(IMainView view, IHelloService helloService)
: base(view)
{
_helloService = helloService;
}
public override void InitalizeData()
{
Message = _helloService.SayHello();
base.InitalizeData();
}
private string _message;
public string Message
{
get { return _message; }
set { _message = value; RaisePropertyChanged("Message"); }
}
}
The View again
The view model is bind to the view datacontext, we can now bind the view model properties to view controls in the XAML code:
<TextBlock Text="{Binding Message}" />
Conclusion
In this article, we have seen that using an IOC cointainer with the MVVM pattern is very usefull and easy in a WP7 project.
You can find the complete source code here: Phone7.fx.IOC-MVVM.zip (139,99 kb)