[This post is a part of a series of post about the Social Media Dashboard Sample. This post was written by Simon Jäger. For an introductory blog post, click here]
Users love animations - it’s a great and a not very time consuming way of introducing more personality into your projects! Using C#/XAML we have been provided with a bunch of different ways of creating liveliness in our apps and games.
Blend for Visual Studio and XAML Storyboards (http://msdn.microsoft.com/en-us/library/ms742868.aspx) are powerful and probably the most well-known approaches for this task.
In this post we’ll be taking a look at using the DispatcherTimer class to produce animations.
The DispatcherTimer class is ideal for our purposes as it is built into the Dispatcher queue with the other work items and operations. Obviously , if there are operations taking too long in the queue – the timer will also have to hold resulting in the animation taking longer than expected.
The great upside with the DispatcherTimer class is that we can run our animation logic directly in the DispatcherTimer.Tick event (http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.dispatchertimer.tick.aspx) instead of having to put the logic into the Dispatcher of the UI thread in order to invoke it.
In the Social Media Dashboard Sample we want to have the existing tiles in the main page to fade into plain colors with a label on it. To achieve this, the first thing that we need to do is to dig into our existing XAML data templates and add the controls that we wish to animate.
The new border element will be overlaying the existing content with its background – which is what we want. The TextBlock element will be on top of the new border control displaying a few lines of wrapped text.
We also need to make sure that the bound XAML properties for the new elements have the corresponding properties in the existing view model, SpotlightFlickrImageDataItem.cs.
Next up we need to add the DispatcherTimer object itself and add some additional properties to the view model class.
The enumeration is built up according to the following:
To continue on, we have to initialize the default values for the properties and hook up the Tick event on the DispatcherTimer. The constructor will be a good place for this task.
The DispatcherTimer is given an interval of 16 milliseconds, meaning the DispatcherTimer will try (potential delays described earlier in this post) to fire the Tick event approximately 60 times per second. Finally we tell the DispatcherTimer to start by calling the Start method.
In the Tick event handler we build our animation logic and modify the appropriate properties to make the XAML elements react as we desire.
Voila! By using timers to build our animations, we get a better understanding of how animations can be built up from the core – this approach is what the XAML Storyboards is using more or less under the hood.
The XAML Storyboards are powerful objects, but as you increase the complexity of your animations you might want to consider using this technique. As it provides a large amount of flexibility and freedom to make finely tuned adjustments down to the core.