动态创建storyboard

来源:互联网 发布:2014年网络零售交易额 编辑:程序博客网 时间:2024/05/21 06:58
<Canvas x:Name="LayoutRoot" Background="White" Loaded="Create_And_Run_Animation"/>
public void Create_And_Run_Animation(object sender, EventArgs e)
        
{
            Ellipse ellipse 
= new Ellipse();
            ellipse.Width 
= 20;
            ellipse.Height 
= 20;

            Color myColor 
= Color.FromArgb(25525500);
            SolidColorBrush myBrush 
= new SolidColorBrush();
            myBrush.Color 
= myColor;
            ellipse.Fill 
= myBrush;
            LayoutRoot.Children.Add(ellipse);

            Duration duration 
= new Duration(new TimeSpan(0,0,0,1));
         
            DoubleAnimation myDoubleAnimation1 
= new DoubleAnimation();
            DoubleAnimation myDoubleAnimation2 
= new DoubleAnimation();

            myDoubleAnimation1.Duration 
= duration;
            myDoubleAnimation2.Duration 
= duration;

            Storyboard sb 
= new Storyboard();

            sb.Duration 
= duration;
            sb.AutoReverse 
= true;
            sb.RepeatBehavior 
= RepeatBehavior.Forever;
            sb.Children.Add(myDoubleAnimation1);
            sb.Children.Add(myDoubleAnimation2);

            Storyboard.SetTarget(myDoubleAnimation1, ellipse);
            Storyboard.SetTarget(myDoubleAnimation2, ellipse);
            Storyboard.SetTargetProperty(myDoubleAnimation1, 
"Width");
            Storyboard.SetTargetProperty(myDoubleAnimation2, 
"Height");
            
            myDoubleAnimation1.From 
= 20;
            myDoubleAnimation2.From 
= 20;
            myDoubleAnimation1.To 
= 50;
            myDoubleAnimation2.To 
= 50;
            
            LayoutRoot.Resources.Add(sb);

            sb.Begin();
        }