In the first part of this post series, I discussed the reasoning behind potentially wanting to have a second way of doing animation in XAML.
In this post, I will discuss the basic infrastructure for this new way of doing thing animation.
The Orchestration class
In the root of the infrastructure reside two classes – the ManagedAnimation and Orchestration classes. Orchestration is a static helper class with some utilities pertaining mostly to how storage of the currently running animations. For each element that’s participating in this new system, it will store a list of currently active ManagedAnimation instances by way of an attached dependency property:
That’s pretty simple. On top of that, we’ll add a couple of helper methods that will make working with this class easier:
First off, the VerifyList method which makes sure there’s a list of ManagedAnimation instances available on a given element:
The relevant DependencyObject is extracted from ManagedAnimation (we’ll see this a bit later) and the method makes sure there’s a list instantiated and available.
Next, we have two methods – one for adding and one for removing a ManagedAnimation to a DepenedencyObject (usually to a FrameworkElement):
The functionality is fairly straightforward. The only interesting bit here is that before adding a ManagedAnimation instance from the list, the call makes sure an equivalent one is not already applied (for more information about Equivalent(), see below). The call also makes sure to call Retain() and Stop() to keep the values that are currently assigned and stop the currently running animation. If the animation was not stopped, the new Storyboard, when called to start, would throw (since you cannot animate the same property of the same object more than by one Storyboard at any given time).
That’s it. We now have a simple utility class that can keep track of the various animations currently running on a given element.
The ManagedAnimation class
This is where things start to get a little more interesting. The ManagedAnimation class is a base class used to apply animation to a given element. It contains the basic management routines and takes care of [most] of the legwork needed.
The class contains a handful of fields/properties:
- m_storyboard: This class contains the animation definition applied by this ManagedAnimation instance. As you will see in a bit, the derived classes are the ones that are going to “fill” it up.
- m_completeResult: This is the value returned when awaiting an animation. It contains some simple information such as whether or not the animation was completed naturally.
- ForceFinalValueRetained: This property specifies whether the animation will “snap” to the final value specified when stopped before it’s complete, or if it’s going to stay where it is. For animation to look “continuous”, this will almost always be false. An example for when this should be true is if there’s an especially long running animation of Opacity and a second animation of Opacity may start at some point before the end. When that’s the case, the developer may pass true for this value, and the infrastructure will make sure Opacity goes to it’s final animation value before starting to animate again.
- FrameworkElement: This is the element the animation will operate on.
Next comes the important bit of this class – the actual activation of the animation:
The activation method sets the various parameters it gets on the class and then proceeds to start the animation. First, it creates the Storyboard by calling the abstract CreateStoryboard() method which will be implemented by the derived classes. It then calls the BeginAsync() extension method on the created Storyboard (more about that one at the end of this post) and awaits it.
This makes the animation sequence start. When the animation is complete, the activate method cleans up after itself – it calls the Orchestration class to remove itself from the element, nullifies a bunch of fields/properties and returns.
The final three interesting methods in the class are:
- CreateStoryboard() we touched on before – inheriting classes do the heavy-lifting there and build the relevant Storyboard.
- Equivalent() is used by the Orchestration class to figure out if one ManagedAnimation class is equivalent to another and as such needs to cancel an existing one.
- Retain() is used at the end of an animation to “nail” the current state of the object so that it will remain there (since we are stopping the animation).
A note about Storyboard.BeginAsync()
BeginAsync() is an extension method available in the Extensions class in the library. It uses a fairly standard mechanism for transforming an event into an awaitable operation (an operation returning a Task instance).
The code for EventToTaskAsync() is also available in the download (which will be available at the final post).
The next post in this series will discuss how to implement the OpacityManagedAnimation class – used to change opacity of objects. The post will also contain the first simple application that will make use of this animation.