在同一时间运行更多的时间表

本文关键字:时间表 同一时间 运行 | 更新日期: 2023-09-27 17:50:57

我试图通过代码在窗口学习WPF中的一些东西,而不是XAML。我想有更多的时间线变换对象(矩形)在同一时间。我的意思是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 and Top移动和旋转也不起作用

在同一时间运行更多的时间表

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

XAML示例:

<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#示例:

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);