Programmatic drawing with Silverlight 2 Part 3: Simple Animation
Posted by Dennis on Mar 12, 2008 in Silverlight • 6 comments
This example shows you how you can make a simple line animation in Silverlight 2. There isn’t much documentation about this yet, so it took some figuring out on how to do this correctly.
Basically these are the steps to create an animation:
- Create a Storyboard object
- Set the Storyboard’s Duration property
- Create a DoubleAnimation object
- Set the DoubleAnimation object’s Duration property
- Add the DoubleAnimation object to the Storyboard object’s children
- Call the static SetTarget method on the Storyboard class and pass it the DoubleAnimation object and the object that will be animated.
- Call the static SetTargetProperty method on the Storyboard class and pass it the DoubleAnimation object and the property of the object that will be animated.
- Set the To property on the DoubleAnimation object. This is object’s property that will be used to animate to.
- Add the storyboard to the layout
- Start the animation by calling Storyboard’s Begin method.
Same steps in code:
Storyboard storyBoard = new Storyboard();
Duration duration = new Duration(TimeSpan.FromMilliseconds(600));
storyBoard.Duration = duration;
DoubleAnimation animation = new DoubleAnimation();
animation.Duration = duration;
storyBoard.Children.Add(animation);
Storyboard.SetTarget(animation, objectToBeAnimated);
Storyboard.SetTargetProperty(animation, "objectToBeAnimated's property");
animation.To = someValue;
LayoutRoot.Resources.Add(storyBoard);
storyBoard.Begin();
Take a look at this example that draws a sequence of lines. I’ve used another property of the DoubleAnimation class: BeginTime. This way all lines are drawn consecutively. The code for this example:
using System;
using System.Collections.Generic;
using System.Linq;
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 LineAnimation
{
public partial class Page : UserControl
{
private Storyboard storyBoard;
public Page()
{
InitializeComponent();
Loaded += new RoutedEventHandler(PageLoaded);
}
private void PageLoaded(object sender, RoutedEventArgs e)
{
CreateAnimation();
storyBoard.Begin();
}
private void CreateAnimation()
{
Line[] lines = new Line[700];
int durationMillis = 200;
Duration duration = new Duration(TimeSpan.FromMilliseconds(durationMillis));
double previousX = 0;
double previousY = 0;
int lineCount = 0;
storyBoard = new Storyboard();
storyBoard.Duration = new Duration(TimeSpan.FromMilliseconds(durationMillis * 60));
for (int i = 0; i < 700; i++)
{
double newX = Math.Sin(i*4) * 150;
double newY = Math.Cos(i*4) * 150;
newX += 200;
newY += 200;
if (i > 0)
{
SolidColorBrush stroke = new SolidColorBrush();
stroke.Color = Colors.Black;
lines[lineCount] = new Line();
lines[lineCount].X1 = previousX;
lines[lineCount].Y1 = previousY;
lines[lineCount].X2 = previousX;
lines[lineCount].Y2 = previousY;
lines[lineCount].Stroke = stroke;
LayoutRoot.Children.Add(lines[lineCount]);
DoubleAnimation animationX = new DoubleAnimation();
DoubleAnimation animationY = new DoubleAnimation();
animationX.BeginTime = animationY.BeginTime = new TimeSpan(0, 0, 0, 0, lineCount * durationMillis);
animationX.Duration = animationY.Duration = duration;
storyBoard.Children.Add(animationX);
storyBoard.Children.Add(animationY);
Storyboard.SetTarget(animationX, lines[lineCount]);
Storyboard.SetTarget(animationY, lines[lineCount]);
Storyboard.SetTargetProperty(animationX, "X2");
Storyboard.SetTargetProperty(animationY, "Y2");
animationX.To = newX;
animationY.To = newY;
lineCount++;
}
previousX = newX;
previousY = newY;
}
LayoutRoot.Resources.Add(storyBoard);
}
}
}




I also would like to learn its performance? Is there any performance consideration here, if there is how did you succeed? It is also true that there arent many documents about silverlight.. thanks for the work
Thanks for sharing! How much of this code was hand-coded versus auto-generated?
for john.. u dont need to care its performance at least for this program… performance is a matter when u deal with memory…
Found a couple problems when translating this to vb.net. the code
Storyboard.SetTargetProperty(animationX, “X2″);
Storyboard.SetTargetProperty(animationY, “Y2″);
is either mityped cause it wouldnt accept the parameters in vb.net and
LayoutRoot.Resources.Add(storyBoard);
what is the originating XAML object that is named LayoutRoot. I am using Canvas to see if this works but you didnt say what XAML objects you were using so we could associate this snippet to the overall picture.
same problems as john, im using c#.
error with:
Storyboard.SetTargetProperty(animationX, “X2″);
Storyboard.SetTargetProperty(animationY, “Y2″);
LayoutRoot.Resources.Add(storyBoard); needs another parameter.
anyone got this to work?
@John & Alex:
This demo was made with a Silverlight Beta version. The API has changed since I created this example. Check the Silverlight documentation for more info.