A while ago I wrote an article describing how the Portable Class Library can enable us to write Unit Tests for our Windows Phone apps using the tools and frameworks we’re already familiar with. By adding an abstraction layer and using types common to the Base Class Library, Portable Class Library and Windows Phone we can write unit tests using tools like MSTest and Moq.

ViewModels and ICommand

A popular approach for writing Windows Phone apps is to use the Model-View-ViewModel pattern and by that expose operations on the View Model as commands which can be data bound to buttons on the view. The interface which enables commands, ICommand, isn’t available in the Portable Class Library thus exposing that kind of commands from our

BindingConverter to the rescue

Instead we can implement a BindingConverter which allow us to dispatch the invocation of a command to a method on the View Model. So far this approach seems to work really well and when giving this solution a second thought I really believe it improved the architecture by separating the concerns even further – the view specific ICommand is no longer part of the model library.

   Content="Button text" 
   Command="{ Binding 
      Converter={ StaticResource CommandConverter }, 
      ConverterParameter='ViewModelMethodName' }" 
   CommandParameter="MethodParameter" />

Thanks to Johan Lindfors who initiated this discussion.

Visual Studio “11”

Today I learnt that Visual Studio “11” Beta has support for ICommand when targeting Windows Phone, Silverlight, Metro and .NET 4.5. Pure awesomeness.

@chrislof Just in case you missed it, we've added support for ICommand in Visual Studio 11 Beta when targeting Phone, SL, Metro + .NET 4.5.

— David Kean (@davkean) March 14, 2012
Blog Logo

Christofer Löf