Monday, 14 March 2016

Xamarin.Forms Behaviors: EventHandlerBehavior and SetPropertyAction

Previously, I demonstrated using the EventHandlerBehavior and InvokeMethodAction classes to invoke one or more methods when an event fires. 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.

In this blog post, I’ll demonstrate using the EventHandlerBehavior and SetPropertyAction classes to set one or more properties when an event fires.

Setting a Property when an 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 SetPropertyAction class sets a specified property when invoked. It requires you to set a TargetObject property to an object that exposes the property of interest, a PropertyName property to the name of the property to be set, and a Value property to an object that represents the property value to be set.

The following code example shows an example of using the EventHandlerBehavior and SetPropertyAction classes to set three properties:

<ContentPage.BindingContext> <local:SetPropertyDemoPageViewModel /> </ContentPage.BindingContext> <Button x:Name="button" Text="Change Page and ViewModel Properties"> <Button.Behaviors> <behaviors:EventHandlerBehavior EventName="Clicked"> <behaviors:SetPropertyAction TargetObject="{x:Reference boxView}" PropertyName="Color" Value="{StaticResource ColorRed}" /> <behaviors:SetPropertyAction TargetObject="{Binding}" PropertyName="MessageText" Value="Message text changed" /> <behaviors:SetPropertyAction TargetObject="{x:Reference button}" PropertyName="IsEnabled" Value="false" /> </behaviors:EventHandlerBehavior> </Button.Behaviors> </Button>

When the Button.Clicked event fires, three properties are set:

  • The Color property of the BoxView instance is set to red.
  • The MessageText property of the SetPropertyDemoPageViewModel instance is set to “Message text changed”. In this case, the TargetObject property specifies that the BindingContext exposes the property of interest. Therefore, the SetPropertyAction class will search the BindingContext of the attached control for the property, which in this case is inherited from the page.
  • The IsEnabled property of the Button instance is set to false.

Note that the Actions property of the EventHandlerBehavior instance is set indirectly by creating the SetPropertyAction instances as children of the EventHandlerBehavior instance.

The advantage of combining the EventHandlerBehavior and SetPropertyAction classes is that properties of controls can easily be set without having to bind them to ViewModel properties, therefore removing boiler-plate properties from ViewModels, and boiler-plate property setting code from code-behind files.

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

Summary

The EventHandlerBehavior class listens for a specified event to occur, and executes one or more actions in response. The SetPropertyAction class sets a specified property when invoked. The advantage of combining the EventHandlerBehavior and SetPropertyAction classes is that properties of controls can easily be set without having to bind them to ViewModel properties, therefore removing boiler-plate properties from ViewModels, and boiler-plate property setting code from code-behind files.

No comments:

Post a Comment