In the previous two posts, I set up the reasoning and infrastructure for creating a different type of animation infrastructure. In this post, I’ll (finally) show an actual example of how to use this infrastructure.
For this, we’ll create a simple application. Our first goal is to place numbered cards (1-5) on our canvas. The cards are represented by a simple UserControl named NumberCardControl which has a Text property specifying the content.
Our initialization code will dynamically create the cards and place them at the bottom of the screen. The final result will look like this:
And this is what the code looks like (it uses a couple of non-standard extension methods to simplify working with the Canvas control):
Right now, the code simply places the cards on the canvas. There’s no entrance animation or anything happening. It’s very abrupt. What if we want to animate these controls in? Say, by changing their opacity from 0 to 1 gradually?
That’s where our infrastructure (finally) enters… First, we’ll need a derived class that knows how to animate opacity:
There are three properties we will use:
- Opacity: This is the target opacity we want the element to arrive at.
- Duration: This is the total duration of the animation.
- EasingFunctionBase: This is the easing function we want the opacity to animate with.
Finally, we have a constructor that sets all of these (and has a default for the easing function).
Next, we need to implement the two virtual functions:
As you can see, Retain is pretty straight-forward. It either takes the target Opacity value or the current Opacity (depending on ForceFinalValueReturned that I discussed in the previous post) and sets it into the property so that when we stop the Storyboard, the element will have the Opacity we actually want.
Next, we need to create the Storyboard. This is fairly simple boilerplate code for anyone who coded Storyboards before, so just to summarize what it does:
- Create an instance of a Storyboard and Timeline classes.
- Tell the timeline what object and property it’s going to animate.
- Create a frame that will specify the animation to cause (with the Value property set to the target opacity).
- Add the frame to the timeline and the timeline to the storyboard.
- return the storyboard.
That’s it! We now have our first animation class and we can start using it!
First, let’s do some simple animation with the cards. We’ll make them all appear on the canvas at the same time, within 1 second of the page appearing.
The code is fairly simple:
We new up an OpacityManagedAnimation class and immediately call the .Activate() method on it.
This is an awaitable operation (Activate() returns a Task) but in this instance we are not even awaiting it because we don’t really need to do anything with it.
Running the app, you will see the the elements appearing together!
Staggering the animation
The animation is pretty boring right now. The cards appear at the same time. What if we wanted to stagger the animation so that they appear one after the other?
Here’s what we are doing:
- We go over the controls, activating the opacity animation just like we did before.
- However, we also await a call to Delay() which will essentially add a short pause before each activation.
- We also collect the animation tasks we added into a list.
- At the end, we await all the animation tasks so that the overall Task will be done only once all the animations are done.
Flickering the elements
To make our example even more complicated, let’s say we want to flicker the elements – go up to 0.6 opacity, then back to 0.2, then to 0.7, then to 0.3 until we reach an opacity of 1 (+0.1 for the target, and go down by 0.4 until we hit 1 as the opacity).
Again – this is a very easy piece of code to run. First, we’ll write up the piece of code that will calculate the opacities:
The code simply loops and makes the calculations I described above – it then calls FlickerToValueAsync()which takes the top value, and the value we want to back down to each time (until we reach full opacity).
The code has two loops in it. The first brings the various elements in the canvas to the “to” opacity (1st argument) and the second loop (which only runs if the target opacity is still less than 1) brings the opacity back down to the backDownTo argument.
In the middle, we add a short delay and that’s it. Voila.
Is this really easier than traditional Storyboards?
The short answer is that it is and it isn’t.
Using this method is more complicated than had you defined your Storyboard in a rigid manner in Blend, for example. However.. What if your list of 5 controls was not rigid? In the code I presented, if you want 10 controls, you simply touch that number in the initialization function and voila! Everything keeps working – no need to adjust any Blend designer or anything. No need to copy animations over in XAML. It’s all written in code and is fully dynamic. The staggered animation, for example, will always be perfectly staggered without having to manually set the timelines in XAML or Blend.
In the next post, I’ll discuss some more ManagedAnimation derivatives you can use to animate elements in your XAML application in a similar manner.