C # 3.0 syntax is used in the code
The effect is a red rectangle moving from the lower-right corner to the upper-left corner
Just an example that shows how to create animations dynamically in code
MainPage.xaml
<UserControl x:Class="Hongcing.Silverlight.Create_And_Run_Animation"
xmlns="http://schemas. microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft. com/winfx/2006/xaml">
<Canvas Loaded="LayoutRoot_Loaded" />
</UserControl>
MainPage.xaml.cs
1 using System;
2 using System.Windows;
3 using System.Windows.Controls;
4 using System.Windows.Media;
5 using System.Windows.Media.Animation;
6 using System.Windows.Shapes;
7
8 namespace Hongcing.silverlight
9 {
Ten public partial class Create_and_run_animation:usercontrol
11 {
Public Create_and_run_animation ()
13 {
InitializeComponent ();
15}
16
-private void Layoutroot_loaded (object sender, RoutedEventArgs e)
18 {
var redrectangle = new Rectangle
20 {
Width = 300,
Height = 200,
Fill = new SolidColorBrush (colors.red),
Stroke = new SolidColorBrush (colors.black)
25};
26
(sender as Panel). Children.add (Redrectangle);
28
var leftanimation = new DoubleAnimation
30 {
Duration = new Duration (Timespan.fromseconds (5)),
From = 700,
to = 0
34};
35
var topanimation = new DoubleAnimation
37 {
Duration = Leftanimation.duration,
From 350,
to = 0
41};
42
Storyboard.settarget (Leftanimation, Redrectangle);
Storyboard.settarget (Topanimation, Redrectangle);
45
46//property path can also be used with new PropertyPath ("(Canvas.Left)"), New PropertyPath ("(canvas.top)")
Storyboard.settargetproperty (Leftanimation, New PropertyPath (Canvas.leftproperty));
Storyboard.settargetproperty (Topanimation, New PropertyPath (Canvas.topproperty));
49
50//This is not added to the resource, but it starts the animation directly.
Wuyi new Storyboard {Children = {leftanimation, topanimation}}. Begin ();
52}
53}
54}
55