Silverlight开发历程—C#代码添加动画

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();
        }
运行结果:会有一个圆在不停的变换形状态和位置。

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...