同时运行更多的时间表

时间:2014-03-19 19:43:35

标签: c# wpf

我试图通过代码而不是XAML在窗口中学习WPF中的内容。我想有更多的时间线来同时转换对象(矩形)。我的意思是由一个命令调用RotateTransform和TransitionTransform。

    Rectangle aRectangle = new Rectangle();
    aRectangle.Width = 30;
    aRectangle.Height = 30;
    aRectangle.Fill = Brushes.Blue;

    TransformGroup g = new TransformGroup();

    RotateTransform rotateTransform = new RotateTransform();
    TranslateTransform animatedTranslateTransform = new TranslateTransform();


    g.Children.Add(rotateTransform);
    g.Children.Add(animatedTranslateTransform);

    aRectangle.RenderTransformOrigin = new Point(0.5, 0.5);
    aRectangle.RenderTransform = g;

    PathGeometry animationPath = new PathGeometry();
    PathFigure pFigure = new PathFigure();
    pFigure.StartPoint = new Point(0, 0);
    seg.Points.Add(new Point(0, 100));
    seg.Points.Add(new Point(100, 100));
    seg.Points.Add(new Point(100, 50));
    seg.Points.Add(new Point(50, 50));
    pFigure.Segments.Add(seg);
    animationPath.Figures.Add(pFigure);
    animationPath.Freeze();

    DoubleAnimationUsingPath translateXAnimation =
        new DoubleAnimationUsingPath();
    translateXAnimation.PathGeometry = animationPath;
    translateXAnimation.Duration = TimeSpan.FromSeconds(5);
    translateXAnimation.Source = PathAnimationSource.X;


    DoubleAnimationUsingPath translateYAnimation =
        new DoubleAnimationUsingPath();
    translateYAnimation.PathGeometry = animationPath;
    translateYAnimation.Duration = TimeSpan.FromSeconds(5);
    translateYAnimation.Source = PathAnimationSource.Y;
    DoubleAnimation translateAngle = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(5)));

如果我使用此代码,一切正常:

    rotateTransform.BeginAnimation(RotateTransform.AngleProperty, translateAngle);
    animatedTranslateTransform.BeginAnimation(TranslateTransform.XProperty, translateXAnimation);
    animatedTranslateTransform.BeginAnimation(TranslateTransform.YProperty, translateYAnimation);

但它似乎非常复杂,我试图了解如何将所有动画组合在一起。我已经尝试过Storyboard但我必须使用Canvas.Left和Top来移动它,并且旋转也不起作用。

1 个答案:

答案 0 :(得分:0)

并行运行动画的最佳方法是将它们全部添加到故事板并启动故事板。这可以在C#和XAML中完成。

XAML from this page中的示例:

<Storyboard x:Key="sb" RepeatBehavior="Forever">
    <DoubleAnimation Storyboard.TargetName="myrect" 
                     Storyboard.TargetProperty="Width" From="1" To="350" 
                     Duration="0:0:1" BeginTime="0:0:0"/>

    <DoubleAnimation Storyboard.TargetName="myrect" 
                     Storyboard.TargetProperty="Height" From="1" To="250" 
                     Duration="0:0:1" BeginTime="0:0:1"/>

    <DoubleAnimation Storyboard.TargetName="myrect" 
                     Storyboard.TargetProperty="Height" From="250" To="1" 
                     Duration="0:0:1" BeginTime="0:0:2"/>

    <DoubleAnimation Storyboard.TargetName="myrect" 
                     Storyboard.TargetProperty="Width" From="350" To="1" 
                     Duration="0:0:1" BeginTime="0:0:3"/>
</Storyboard>

C# from this page中的示例:

DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 100;
myDoubleAnimation.To = 200;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty));

ColorAnimation myColorAnimation = new ColorAnimation();
myColorAnimation.From = Colors.Blue;
myColorAnimation.To = Colors.Red;
myColorAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
Storyboard.SetTargetName(myColorAnimation, "MySolidColorBrush");
Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty)); 
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);
myStoryboard.Children.Add(myColorAnimation);

myStoryboard.Begin(this);