XAML:
<Canvas x:Name="LayoutRoot" Background="White"> <Canvas x:Name="canvas_parent" /> </Canvas>
C#:
public AnimationWithCSharp() { InitializeComponent(); //创建椭圆对象 Ellipse ellipse = new Ellipse(); ellipse.Width = 150; ellipse.Height = 150; ellipse.Fill = new SolidColorBrush(Color.FromArgb(255,255,0)); //添加到Canvas中 canvas_parent.Children.Add(ellipse); //创建Double动画 DoubleAnimation animation1 = new DoubleAnimation(); DoubleAnimation animation2 = new DoubleAnimation(); DoubleAnimation animation3 = new DoubleAnimation(); //设置动画的From To属性 animation1.From = 20; animation1.To = 400; animation2.From = 20; animation2.To = 250; animation3.From = 30; animation3.To = 150; //设置动画时间 animation1.Duration = new Duration(new TimeSpan(0,1)); animation2.Duration = new Duration(new TimeSpan(0,1)); animation3.Duration = new Duration(new TimeSpan(0,1)); //创建故事面板 Storyboard sb = new Storyboard(); //设置故事面板的时间 sb.Duration = new Duration(new TimeSpan(0,1)); //设置动画是否重复播放 sb.RepeatBehavior = RepeatBehavior.Forever; //添加三个动画到故事面板中 sb.Children.Add(animation1); sb.Children.Add(animation2); sb.Children.Add(animation3); //设置动画的作用目标 Storyboard.SetTarget(animation1,ellipse); Storyboard.SetTarget(animation2,ellipse); Storyboard.SetTarget(animation3,ellipse); //设置动画作用属性 Storyboard.SetTargetProperty(animation1,new PropertyPath("(Canvas.Left)")); Storyboard.SetTargetProperty(animation2,new PropertyPath("(Canvas.Top)")); Storyboard.SetTargetProperty(animation3,new PropertyPath("Height")); //添加故事板到,Canvas中 canvas_parent.Resources.Add("storyboard",sb); //开始播放动画 sb.Begin(); }运行结果:会有一个圆在不停的变换形状态和位置。