WinRT and ViewModel-first: XAML is not enough!

Topics: Getting Started
Nov 30, 2012 at 2:26 PM

After some hours of head scratching and debugging, I have finally managed to successfully run a simple ViewModel-first Caliburn application for WinRT!

I thought I'd share my experiences in case it can be of help to someone, and also in case someone knows a better workaround than mine.

Initially, I created a very simple MVVM app, button and a text box, and followed the instructions on this page to set up the App.xaml and App.xaml.cs files.

Initially I set up my application as a View-first application, and it ran without problems. Next, I followed the instructions for the ViewModel-first approach (remove PrepareViewFirst override and use DisplayRootViewFor in OnLaunched). However, when I then ran the application, I only faced the following message:

Cannot find view for MyApplication.ViewModels.ShellViewModel

After some "serious" debugging I managed to find the root cause: I had removed the ShellView.xaml.cs file! As a consequence of this, the ShellView class was not included in the assembly and could not be found by the ViewLocator.

My workaround to this failure was to add a dummy ShellView.xaml.cs file with only a partial class declaration in it. With this dummy modification of my project, the view successfully showed up when running the application.

I removed the .xaml.cs file in the first place since until now I was pretty sure this was the recommended procedure when developing Caliburn Micro applications. For example, I believe that in most ViewModel-first CM samples there are no .xaml.cs files.

Granted, I have not verified that the View classes are included in the assemblies in non-WinRT applications, but I get the feeling that WinRT/Windows Store applications are built and compiled more aggressively. Can someone confirm or refute this, and if confirmed, does anyone know how to "reduce this aggressiveness"?