我是WPF新手,我只想逐个动画两个对象。这意味着第二个动画应该在第一个完成后开始。我尝试使用不能正常工作的Timer和Sleep方法。以下是示例代码:
DoubleAnimation da1 = new DoubleAnimation()
{
From = 10,
To = 200,
Duration = new System.Windows.Duration(TimeSpan.FromSeconds(sec)),
};
temLabel1.BeginAnimation(Canvas.TopProperty, da1);
Delay(); // Delay when busy
DoubleAnimation da2 = new DoubleAnimation()
{
From = 300,
To = 500,
Duration = new System.Windows.Duration(TimeSpan.FromSeconds(sec)),
};
ItemLabels2.BeginAnimation(Canvas.LeftProperty, da2);
我正在使用Timer
在两个动画之间创建时间延迟
/* Setting Timer For delay during animation */
timer.Interval = sec * 1000 + 50;
timer.Elapsed += Timer_Elapsed;
延迟功能的代码是:
void Delay()
{
busy = true;
timer.Start();
while (busy) {}
}
计时器已用事件处理程序
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
busy = false;
timer.Stop();
}
请纠正我错在哪里。我不想为此目的使用Note:DoubleAnimation.Completed
事件处理程序,因为在通过循环处理和动画化对象集合时会很困难。
非常感谢您的回复。
答案 0 :(得分:3)
试试这个:
private int sec = 2;
private async Task StartAnimation()
{
await StartAnimationForLabel1();
await StartAnimationForLabel2();
}
private async Task StartAnimationForLabel1()
{
DoubleAnimation da1 = new DoubleAnimation()
{
From = 10,
To = 200,
Duration = new System.Windows.Duration(TimeSpan.FromSeconds(sec)),
};
ItemLabel1.BeginAnimation(Canvas.TopProperty, da1);
await Task.Delay(sec * 1000);
}
private async Task StartAnimationForLabel2()
{
DoubleAnimation da2 = new DoubleAnimation()
{
From = 300,
To = 500,
Duration = new System.Windows.Duration(TimeSpan.FromSeconds(sec)),
};
ItemLabels2.BeginAnimation(Canvas.LeftProperty, da2);
await Task.Delay(sec * 1000);
}
了解有关使用Async和Await进行异步编程的更多信息,请参阅Here
需要.NET Framework 4.5或更高版本
答案 1 :(得分:0)
您至少有两个选项
我认为选项1适合您的需求。