微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐