Previously, I demonstrated using the EventHandlerBehavior and FadeAction classes to run a fade animation when an event occurs. 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 TranslateAction classes to run a translation animation when an event occurs.
Translating a VisualElement 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. Note that the Actions property of the EventHandlerBehavior instance is set indirectly by creating the TranslateAction instance as a child of the EventHandlerBehavior instance.
The TranslateAction class performs a translation animation, which moves a VisualElement, when invoked, and allows the following optional properties to be set:
- TargetObject – an object that is the VisualElement on which to run the animation. If this value is omitted the object the behavior is attached to will be set as the target on which to run the animation.
- X – a double that specifies the number of pixels to move the TargetObject horizontally. If this value is omitted the default value of 1 is used.
- Y – a double that specifies the number of pixels to move the TargetObject vertically. If this value is omitted the default value of 1 is used.
- Duration – an int that represents the length of the animation in milliseconds. If this value is omitted the default value of 250ms is used.
- EasingFunction – an EasingFunction that specifies any velocity changes in the animation. If this value is omitted the default value of the linear easing function is used.
- Await – a boolean that represents whether the animation should be awaited, or whether it should be allowed to complete in the background. If this value is omitted the default value of false is used.
The following code example demonstrates using the EventHandlerBehavior and TranslateAction classes to implement a compound animation that moves an Image control around the screen when a Button is clicked:
When the Button.Clicked event fires, a series of TranslateAction instances creates a compound animation, which is a sequential combination of animations. Specifically, an Image is translated around the screen over 5 seconds (5000 milliseconds). The translation of the Image uses five translation animations, with the Await property of each TranslateAction instance indicating that each animation executes sequentially. Therefore, subsequent animation methods execute after the previous method has completed,
The advantage of using the TranslateAction is that it’s possible to invoke animations through XAML, rather than having to use C#. In addition, when combined with behaviors, a translation animation can easily be invoked from XAML when a behavior occurs, such as an event firing, or a when a piece of data changes.
The sample application that this code comes from can be downloaded from GitHub.
The TranslateAction class allows translation animations to be invoked through XAML when a behavior occurs, such as an event firing, or when a piece of data changes.