Tuesday, 26 April 2016

Xamarin.Forms Behaviors: Support for Page Level Behaviors

Previously, I’ve introduced by Behaviors Library for Xamarin.Forms, which can be downloaded from GitHub and NuGet. The Behaviors Library for Xamarin.Forms has the notion of behaviors and actions. A behavior is attached to a control and listens for something to happen, such as an event firing. When the “something” happens, it triggers one or more actions, such as invoking a method or command. Actions are invoked by behaviors and executed on a selected control.

A problem with my behaviors library was that it only allowed behaviors to be attached to views (controls), and not to pages. Therefore, I’ve updated the library so that behaviors can be attached to pages.

In this blog post, I’ll demonstrate using the EventHandlerBehavior and InvokeCommandAction classes to invoke a command when a page event fires.

Invoking a Command when a Page Event Fires

The EventHandlerBehavior class listens for a specified event to occur, and executes one or more actions in response. It requires you to set an EventName property to the event that you want the behavior to listen to, and an Actions property to one or more actions that should be executed in response to the event firing.

The InvokeCommandAction class executes a specified ICommand when invoked. It requires you to set a Command property to an ICommand instance, and CommandParameter and Converter properties can be optionally set. The CommandParameter property should be set to an object instance, with the Converter property being set to an instance of a class that implements IValueConverter. The ICommand specified in the Command property will then be executed, with the CommandParameter and Converter values being used if specified.

The following code shows an example of using the EventHandlerBehavior to invoke a command:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:behaviors="clr-namespace:Behaviors;assembly=Behaviors" x:Class="Behaviors.Sample.InvokeCommandDemoPage" Title="InvokeCommand Demo" Icon="xaml.png"> <ContentPage.BindingContext> <local:InvokeCommandDemoPageViewModel /> </ContentPage.BindingContext> ... <ContentPage.Behaviors> <behaviors:EventHandlerBehavior EventName="Appearing"> <behaviors:InvokeCommandAction Command="{Binding PageAppearingCommand}" /> </behaviors:EventHandlerBehavior> <behaviors:EventHandlerBehavior EventName="Disappearing"> <behaviors:InvokeCommandAction Command="{Binding PageDisappearingCommand}" /> </behaviors:EventHandlerBehavior> </ContentPage.Behaviors> ... </ContentPage>

When the Page.Appearing event fires, the PageAppearingCommand is executed. Similarly, when the Page.Disappearing event fires, the PageDisappearingCommand is executed. The InvokeCommandAction class expects to find the ICommand instances on the BindingContext of the attached object, and in this case the BindingContext is set to an instance of the InvokeCommandDemoPageViewModel class. Note that the Actions property of the EventHandlerBehavior instances is set indirectly by creating each InvokeCommandAction instance as a child of each EventHandlerBehavior instance. When the ICommand instances are executed, they display alerts that confirm that the appropriate page event fired.

The sample application that this code comes from can be downloaded from GitHub.

Summary

The Behaviors Library for Xamarin.Forms now supports attaching behaviors at the page level. This enables actions to be executed in response to a page event firing.

No comments:

Post a Comment