简体   繁体   English

如何在WPF中的循环中使用DoubleAnimation?

[英]How can i use DoubleAnimation in a loop in WPF?

I want to use animation in a for loop. 我想在for循环中使用动画。 But I can only see the last animation. 但是我只能看到最后一个动画。

According to my search I have to use storyboard. 根据我的搜索,我必须使用情节提要。 My code is below. 我的代码如下。

Random rnd = new Random();
Storyboard sb = new Storyboard();

public MainWindow()
{
    InitializeComponent();

    for (int i = 1; i < 4; i++)
    {
        int temp = rnd.Next(1-3);
        move(cisim, 10*temp, 20*temp, 3);
    }
}

public void move( Image target,double oldX,double newX,int time)
{
    DoubleAnimation anim2 = new  DoubleAnimation(oldX,newX,TimeSpan.FromSeconds(time));

    Storyboard.SetTarget(anim2, target);
    Storyboard.SetTargetProperty(anim2, new PropertyPath("(Canvas.Left)"));

    sb.Children.Add(anim2);
    Canvas.BeginStoryboard(sb);
}

I want to see animation 3 times. 我想看动画3次。 For example: 例如:

Animation1->start - finish (after) Animation2->start - finish.    

When I run my code, I just see the last animation. 运行代码时,我只看到最后一个动画。 What is the point I missed? 我错过了什么呢?

You need to set begintime in the second onwards to give previous ones time to do their thing. 您需要在第二个以后设置开始时间,以便给以前的时间做他们的事情。

https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.animation.timeline.begintime?view=netframework-4.8#System_Windows_Media_Animation_Timeline_BeginTime https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.media.animation.timeline.begintime?view=netframework-4.8#System_Windows_Media_Animation_Timeline_BeginTime

At the moment all of your animations begin immediately. 目前,您所有的动画均立即开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM