One powerful feature of silverlight is the ability to quickly and easily build animations into your application. Animations are typically built out using a Storyboard control either in your XAML or programatically in C#. Your storyboard will contain an animation and that animation will be applied to an element. Animations can be DoubleAnimation, ColorAnimation or a PointAnimation. Today we will just focus on the DoubleAnimation.
Double Animations
A DoubleAnimation is simply a double that is either incremented or decremented and is applied to a property. So before we get any further, lets take a look at what it can do:
Just show it to me
As you can see this is smoothly animated to show or hide a simple gray box. The box is just a stack panel that could potentially hold more elements. Here’s the code:
XAML
<UserControl x:Class="SilverlightApplication1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <Canvas Background="White" Margin="0,0,34,108" x:Name="mycanvas"> <Canvas.Resources> <Storyboard x:Name="ShowStoryboard"> <DoubleAnimation Storyboard.TargetName="mystackpanel" Storyboard.TargetProperty="(Canvas.Top)" From="-50" To="0" Duration="0:0:.5" > <DoubleAnimation.EasingFunction> <BackEase Amplitude=".5" EasingMode="EaseOut" /> </DoubleAnimation.EasingFunction> </DoubleAnimation> </Storyboard> <Storyboard x:Name="HideStoryboard"> <DoubleAnimation Storyboard.TargetName="mystackpanel" Storyboard.TargetProperty="(Canvas.Top)" From="0" To="-50" Duration="0:0:.5"> <DoubleAnimation.EasingFunction> <BackEase Amplitude=".5" EasingMode="EaseIn" /> </DoubleAnimation.EasingFunction> </DoubleAnimation> </Storyboard> </Canvas.Resources> <StackPanel Canvas.Top="-50" Height="50" Width="640" VerticalAlignment="Top" x:Name="mystackpanel" Background="Gray"> </StackPanel> <Button Content="Show" Height="23" Name="button1" Width="75" Click="button1_Click" Canvas.Left="20" Canvas.Top="137" /> <Button Canvas.Left="110" Canvas.Top="137" Content="Hide" Height="23" Name="button2" Width="75" Click="button2_Click" /> </Canvas> </Grid> </UserControl>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SilverlightApplication1 { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { if (Canvas.GetTop(mystackpanel) == -50) { ShowStoryboard.Begin(); } } private void button2_Click(object sender, RoutedEventArgs e) { if (Canvas.GetTop(mystackpanel) == 0) { HideStoryboard.Begin(); } } } }
Explanation
As you can see in the XAML code we have 2 storyboards. One to show the gray square and 1 to hide the gray square. The only thing we have in our C# code is to simply start each animation if the animation is shown or hidden. Notice that our stack panel is in a canvas to move it precisely where we want it. With a DoubleAnimation there are 5 major pieces:
#1 Set the Target Name: The target name is the ID of the element we want to animate.
#2 Set the target property: The target property is the property we want to set the double property to. In this case it is (Canvas.Top) <- note the parenthesis when you have a multipart property.
#3 Set the From property: The from property is the starting value.
#4 Set the To property: The to property is the ending value.
#5 Set the duration: Hours:Minutes:Seconds.
You will also notice that we also have a nested EasingFunction. We will go over the easing functions later in the series.
The end?
This is just to ‘wet your whistle’ and show you a basic animation that silverlight can do. I will be posting up more on silverlight animations soon.
