我已经在一个方法中启动了一个动画,之后我将不得不停止并立即完成它,如果它还没有完成的话。
Vector diff = ...
DoubleAnimation aniX = new DoubleAnimation(diff.X, TimeSpan.FromSeconds(0.4));
DoubleAnimation aniY = new DoubleAnimation(diff.Y, TimeSpan.FromSeconds(0.4));
aniX
和aniY
如果尚未完成则停止。
答案 0 :(得分:1)
我认为您应该使用XAML
代码与一些附加的Trigger
来阻止它。以编程方式停止动画。您可以尝试使用Begin
开始Storyboard
和Stop
来停止它(不要使用BeginAnimation
):
Storyboard st = new Storyboard();
Storyboard.SetTarget(aniX, yourObject);
Storyboard.SetTargetProperty(aniX, yourPropertyPath);
st.Children.Add(aniX);
Storyboard.SetTarget(aniY, yourObject);
Storyboard.SetTargetProperty(aniY, yourPropertyPath);
st.Children.Add(aniY);
//begin
st.Begin();
//stop
st.Stop();
注意:上面的代码假设您在过程代码中声明了所有DoubleAnimations
和Storyboard
(不在XAML代码中)。我认为您应该在XAML
代码中声明它们以使其更简洁,WPF
中的许多类和内容都针对XAML
进行了优化,所以有时您会觉得查看它们有点奇怪在程序代码中。