Core Series Part 2 – Automatic template selection – marrying a view to a view model

This is the second part of my series on a different approach to MVVM. I highly recommend you read part one to get some background.

This series introduces concepts around MVVM – mainly around view and viewmodel selection, navigation and transitions. The concepts here can be applied to WP7, Silverlight or WPF (including Surface). Further on in the series I will demonstrate how simple more advanced topics like circular navigation and tombstoning can be made (very) simple. All of the techniques I talk about in this series are implemented in XamlingCore – an simple reference MVVM framework. You may choose to use XamlingCore or take the concepts from this series and use them as you please. I recommend you at least have the XamlingCore code available on your system as I will reference files here and there from the framework.

Keeping things simple

A big part of MVVM is figuring out how to match up the view to the view models that you create. Typically for each view, there is a view model (simple right??). Making this process as intuitive and straightforward as possible is very important.

XamlingCore works a little bit different than you may be used to. In XamlingCore you first create a viewmodel and then navigate to your new viewmodel… this is a little different than other systems – where you first create a view and the viewmodel is then “discovered”. This is called ViewModel first navigation.

I really like ViewModel first navigation – for a few simple reasons. Firstly – your code is completely agnostic to what the view is doing… in fact you could create all your ViewModels and navigate around your application without any views being generated at all! This makes it really easy to test. Secondly the very fact that the ViewModel side is completely view agnostic means that you are achieving a very high level of separation of concerns. It also allows for a really simple navigation system (you just keep your ViewModels in a stack, then pop them as the user navigates back).

The main reason I like this method however is that you create ViewModels just like you would any other object, calling constructors, properties etc to pass around data – and I’ll show some great things you can do with this later.

It may sound a little complex, but in practice it’s actually *really* simple.

ViewModel/View Marriage

The main concept we want to achieve is that ViewModels only worry about themselves, and the view side of the fence handles everything to do with choosing how to display the ViewModels (which templates, user controls etc to choose). This keeps the ViewModels very simple. The view stuff makes sense on the “view side”.

At the top level of our application there will be a NavigationFrame – much like the in built navigation system in Silverlight/WP7. The navigation frame has both View and ViewModel side components. It is responsible for taking the current ViewModel and displaying the appropriate view.

The basic workflow is: Construct new ViewModel -> Navigate to it -> Navigation presents the ViewModel to the frame -> frame resolves and creates view (via a DataTemplate) -> hands off ViewModel and new DataTemplate to a content control for display.

A really simple example

I have prepared a really simple example that demonstrates this basic workflow to give you a more hands on idea about how this would work.

Download the sample here.

View the quick screen cast here.

Obviously this sample wouldn’t work very well in the real world, but it demonstrates the concept of choosing a template based on the ViewModel type that is being shown in the ContentControl.

In the next part we will move on to a more automated, convention based approach!

Happy MVVMing!

One thought on “Core Series Part 2 – Automatic template selection – marrying a view to a view model

Comments are closed.