Introducing Windows.UI.Interactivity: Behaviors for the Windows Runtime

Update: a NuGet package is now available here.

In the Windows Runtime and Blend for Visual Studio 2012 there are no behaviors. This brings lots of problems when you are using the MVVM pattern or you come from the Windows Phone or Silverlight platform. Windows.UI.Interactivity tries to fill this gap by porting the whole System.Windows.Interactivity assembly, where the behaviors live in the Blend SDK, to the Windows Runtime. The project builts on work done by Windows Phone MVP Joost van Schaik a.k.a. LocalJoost and Silverlight MVP Andrea Boschin.

What is a behavior?

A behavior can be seen as an extension method for XAML. Behaviors leverage the power of attached properties and by using them you can extend the functionality of a control without modifying that controls' code.

Why would I care?

When using the MVVM pattern, you want to separate the view from the model. You bind the view to the viewmodel using the powerful data binding properties and the viewmodel should handle the interactions between the view and the model. Unfortunately you can not bind events easily to the viewmodel. This is where behaviors come in. Behaviors can be bound to the viewmodel and they also have access to the object they were attached to. In this way a behavior can register for an event and update a property on the viewmodel or fire a command, whenever an event fires. This is basically what EventToCommand from Laurent Bugnion does. By using Windows.UI.Interactivity you can again use behaviors as before and it also makes porting Windows Phone apps easier.

Great! How do I use it?

Using the library is very simple. Just use it as if it was System.Windows.Interactivity from the Blend SDK. Of course you need to update the namespace to the new Windows Runtime format (xmlns:i="using:Windows.UI.Interactivity"), but that is all you need to do! Any existing behaviors also need to be recompiled to work with this library. To get you started, a sample application is included with a basic ClickBehavior to show how it all works. Just download the code from github and add a reference to it to your app or install the library via NuGet.

Did you like this? Share it: