Previously, I demonstrated using the EventHandlerBehavior and SetPropertyAction classes to set one or more properties 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 DataChangedBehavior and InvokeCommandAction classes to invoke one or more commands when data changes.
Invoking a Command when Data Changes
The DataChangedBehavior class listens for the bound data to meet a specified condition, and executes one or more actions in response. It requires you to set the following properties:
- Binding – an object that represents the bound object that the behavior will listen to.
- ComparisonCondition – a ComparisonCondition enumeration value that represents the comparison to be performed between the values of the Binding and Value properties. The enumeration values are: Equal, NotEqual, LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual.
- Value – an object that represents the value to be compared with the value of the Binding property.
- Actions – one or more actions that should be executed in response to the bound data changing. Note that this property is set indirectly.
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 DataChangedBehavior to invoke a command:
When the value of the Entry.Text property becomes equal to “Xamarin”, the OutputMessageCommand is executed. The InvokeCommandAction class expects to find the ICommand instance on the BindingContext of the attached object, and in this case the BindingContext has been set by a parent element to an instance of the InvokeCommandDemoPageViewModel class. The CommandParameter passes the value of the Entry.Text property to the Execute delegate of the Command. Note that the Actions property of the DataChangedBehavior is set indirectly by creating the InvokeCommandAction instance as a child of the DataChangedBehavior instance.
The advantage of using the DataChangedBehavior and InvokeCommandAction classes is that commands can be executed in response to data changing, removing boiler-plate code from code-behind files.
The sample application that this code comes from can be downloaded from GitHub.
The DataChangedBehavior class listens for the bound data to meet a specified condition, and executes one or more actions in response. The InvokeCommandAction class executes a specified ICommand when invoked. By using these two classes, commands can be executed in response to data changing, removing boiler-plate code from code-behind files.