February 21, 2013

Caliburn Micro: Beware the default Windows Store app templates

Hey, before you start reading! I am in the market, looking for new freelance employment opportunities. If you need assistance on any of your ASP.NET Core projects, I am available for hire for freelance work.

I was recently dumbfounded by an apparent issue with Caliburn Micro in a Windows Store application being unable to locate the view model for a view.  The symptoms was that the application navigated correctly to my view, but the data from the view model was not being displayed on the page.

What complicated matters a bit more was that my views and view models were located in different assemblies and non-default namespaces, therefore I immediately suspected the problem was located there.  After some debugging I determined that Caliburn Micro was indeed locating and instantiating the correct view model for my view, but somehow the view model was not bound to the view.

My application was using the view first approach, so on application launch the following code was executed.

protected override void OnLaunched(LaunchActivatedEventArgs args)

I decided to switch it around to the view model first approach by changing my OnLaunched() method and all of a sudden it worked.

protected override void OnLaunched(LaunchActivatedEventArgs args)

I knew that was not a proper solution and did some more digging.  The culprit turned out to be the fact that the templates for Windows Store applications in Visual Studio sets the DataContext of a page to the DefaultViewModel property of the LayoutAwarePage class and somehow CM does not set the DataContext of a page if it has already been set.


The solution turned out to be as simple as deleting the offending line of markup and after that Caliburn Micro was setting the DataContext correctly and the data was displayed on the page.

There is a discussion on the Caliburn Micro website which describes this issue and the reason behind it.

Follow me on Twitter @jerriepelser for tweets about coding and building a Saas.