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:
Behaviours
- 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.
Actions
- 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 2.0.1.6505.
Scenarios
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.
Wrapping Up
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.
Hi David!
ReplyDeleteIs it also possible to attach behaviors without using the Behaviors property of a view?
With Corcav you can do something like this:
I was wondering if something similar is possible for your library too.
No. Not with this library. That was a deliberate design choice by myself, as I didn't see the need to attach a behavior to an element that doesn't have a Behaviors collection.
DeleteWith that said, I realise that only being able to attach behaviours to a view is a limitation. It's not much work to extend the library so that behaviours can also be attached to pages, so I'll be updating the library soonish.
Would be useful, but only if you have enough time of course. The reason I was looking into your library is because I was looking for an alternative to Corcav. For some reason that one crashes the app when building with .Net Native enabled.
DeleteThe library now supports attaching page level behaviors. I've updated the sample to demo using Page.Appearing/Disappearing events.
DeleteThanks! Will give it a try the next time I need it or when I update one of my projects.
DeleteI love Behaviors.Forms library. I use it on my production app DAILY. Thanks :)
ReplyDeletePlease keep updating it.
Hi David,
ReplyDeleteIn "the old days" (ending last december :-)) I was used to this, in combination with Prism framwork, where this works excellent. I am anxious to discover how this works in today's Xamarin.Forms application. We used to rely heavily on this technique.
Glad you provided this! Thanks
David, is it possible to use your package without XAML (UI written in C#)?
ReplyDeleteYes.
DeleteDavid, will you port this library to netstandard 2?
ReplyDeleteYes, I'll do it this week/next week.
DeleteDavid, can you traslate this code in csharp.. thanks
ReplyDelete