WinRT LayoutAwarePage

Topics: Getting Started, UI Architecture
Nov 15, 2012 at 12:52 PM

Just getting started with WinRT/CaliburnMicro and hit something I'm not sure how best to handle.  I've added the pre-built "Grouped Items Page" which then adds/inherits from LayoutAwarePage.  In the LayoutAwarePage constructor there is the following code:

this.DefaultViewModel = new ObservableDictionary<String, Object>();

Because of this line of code, when CaliburnMicro hits the OnNavigated event and calls LocateForView it finds the DataContext is already set to this DefaultViewModel so just returns that instead of loading the ViewModel for my View.  By commenting out the above line from the LayoutAwarePage constructor CaliburnMicro loads my ViewModel as expected.  So that is what I'm currently doing, but not sure what negative effects it may have or if there is another method I should be using.

Any ideas?


Nov 16, 2012 at 8:52 AM

LayoutAwarePage in the templates takes on a lot of responsibilities in the templates. It does the following

  1. Changes the pages visual state based on the application view (Snapped, Landscape etc).
  2. Listens for keyboard and mouse interactions to provide shortcuts (Alt + Left Arrow for back).
  3. Creates a default "view model" property bag.

Probably a bit much for a single class and personally I'm not a big fan of it. While I like the idea of the visual states the other two shouldn't really be in there.

Regarding the default view model code, if you're using Caliburn Micro the default view model stuff won't be necessary. Personally I think it's terrible that it's in there in the first places as their "view model" is just a property bag, this really defeats the whole purpose of MVVM about splitting view and view model and just kinda throws them back together. My recommendation is to keep the first two pieces of functionality and happily delete the third. If you're embracing Caliburn Micro none of that default view model code will be any help.

Nov 16, 2012 at 9:36 PM

Thanks for the prompt reply!  I'd just commented out code related to default view model and all seemed to work as I expected, but was a little scared something would popup later.  Good to know all should work normally with Caliburn Micro without that stuff.

Thx again!