29Jun/124

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:
Comments (4) Trackbacks (1)
  1. Hi Johan,

    Can you just clarify the license? I might be interested to use your library for EventToCommand in MVVM Light VNext. MVVM Light uses the MIT license which I find perfect for open source when you don’t want to set constraints on the users. Anyway, let me know.

    Cheers,
    Laurent

  2. Why cannot I use Windows.UI.Interactivity in my XAML?

    xmlns:i=”using:Windows.UI.Interactivity”

    just says “Undefined namespace.”

    I can’t even say:
    using Windows.UI.Interactivity;

    in the codebehind, it just says: “The type or namespace ‘Interactivity’ does not exist in the namespace ‘Windows.UI'”

  3. The Command Parameter is a complete mess with Windows.UI.Interactivity. Also I noticed, when I specify the Command parameter type as object, it gives me the EventArgs through command parameter. That is completely unacceptable and violating MVVM in PCL environment, where I dont want to refer any UI related namespace in my Viewmodel.


Leave a comment