In this short tutorial on using the Behaviors SDK in your Windows Phone 8.1 application, I'm going to show you how you can quickly bring your app up to scratch with MVVM by binding to events using Behaviors.
The Behaviors SDK has been around for quite some time for Windows Store apps but with the introduction of the universal app model, it has seen its way onto the Windows Phone platform.
Before we start, this tutorial requires that you have some knowledge of the MVVM pattern and that your project is set up in this method with a view and accompanying view model.
To get yourself started with Behaviors, you'll want to add a reference to the SDK in your project from the Extensions sub-category of Windows Phone 8.1.
Now that you've referenced the SDK, you're going to want to add it into your XAML where you want to use them. For this tutorial, I'm going to show you how you can bind a command to a 'Tapped' event on a control. Usually you would have to create a method within the code behind of your page to handle these types of events, but the use of Behaviors means that we can send the fired event off to the view model.
You'll need to add two namespaces to your XAML page and these are:
- xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
- xmlns:core="using:Microsoft.Xaml.Interactions.Core"
Where you've declared a control in your XAML, open it with a closing tag if you've not already. Within that element, you want to declare a collection of Behaviours that you're going to bind to that control with the following lines of XAML:
- <interactivity:Interaction.Behaviors>
- </interactivity:Interaction.Behaviors>
Now, within our behaviors collection, we want to add the EventTriggerBehavior as a child as so:
- <interactivity:Interaction.Behaviors>
- <core:EventTriggerBehavior EventName="Tapped">
- </core:EventTriggerBehavior>
- </interactivity:Interaction.Behaviors>
Within this behaviour, we are now going to add an action that will be fired when the EventTriggerBehavior is fired. The action we will be using for this tutorial is the InvokeCommandAction that is used to bind to an ICommand property on your view's view model. This will look similar to this in your own project:
- <interactivity:Interaction.Behaviors>
- <core:EventTriggerBehavior EventName="Tapped">
- <core:InvokeCommandAction Command="{Binding CaptureCommand}" />
- </core:EventTriggerBehavior>
- </interactivity:Interaction.Behaviors>
If you'd like to see this in action, you can download the attached sample code that provides everything learned in this tutorial.
Have any questions? Let me know in the comments section below and I will get back in touch!

Join the conversation! Your thoughts help the community grow.