Back in January I wrote a series of blog posts for the Xamarin blog. This included a post on Turning Events into Commands with Behaviors. As part of that blog post I developed a ListViewSelectedItemBehavior, for executing a command when the ListView.ItemSelected event fires. Then at the end of the blog post I mentioned that I’d generalised the ListViewSelectedItemBehavior into an EventToCommandBehavior that could be used to execute a command in response to any event firing.
This got me thinking about how I used to use behaviours when I was developing using Microsoft’s XAML. There, behaviours are attached to a control and listen for something to happen. This something would typically be an event firing or data changing. When the “something” happens, it triggers one or more Actions. Actions are invoked by Behaviours and execute on a selected control. Typical Actions included invoking a command, invoking a method, setting a property etc. These behaviours and actions were made available through the Blend SDK.
I took my EventToCommandBehavior implementation and was able to refactor it into an EventHandlerBehavior and an InvokeCommandAction. This meant I was able to invoke multiple commands in response to an event firing, as demonstrated in the following code example:
I then started thinking about other Behaviours and Actions that I commonly used from the Blend SDK, and after realising how much I missed them in Xamarin.Forms, I decided to implement them. The result is my Behaviors for Xamarin.Forms library.
Introducing Behaviors for Xamarin.Forms
Behaviors for Xamarin.Forms is a PCL I’ve created that can be consumed by Xamarin.Forms applications. It contains the following Behaviours and Actions:
- EventHandlerBehavior – listens for a specified event to occur, and executes one or more Actions in response.
- DataChangedBehavior – listens for the bound data to meet a specified condition, and executes one or more Actions in response.
- InvokeCommandAction – executes a specified ICommand when invoked.
- InvokeMethodAction – executes a method on a specified object when invoked.
- SetPropertyAction – changes a specified property to a specified value.
Please note that the library is something I’ve created in my spare time, and isn’t an official Xamarin release. It’s open source, and is available on GitHub, and a NuGet package can be found here. Note that the NuGet package has a dependency of >= Xamarin.Forms 188.8.131.5205.
The library supports scenarios such as:
- Invoking commands when an event fires, or when data changes.
- Invoking methods (View or ViewModel) when an event fires, or when data changes.
- Setting properties (View or ViewModel) when an event fires, or when data changes.
For examples of these scenarios, see the accompanying sample application that executes Actions against Views and ViewModels.
The result of using the library is that you can eliminate lots of boiler plate C# code, instead moving it to XAML.
Behaviors for Xamarin.Forms is a PCL library that can be used by Xamarin.Forms applications to eliminate lots of boiler plate C# code, instead moving it to XAML.
Over the coming weeks I’ll publish blog posts that explore each Behaviour and Action, explaining how they work, and any limitations.
Questions? Problems? Thoughts? Suggestions? Then drop me a line.