如果还没完成,如何立即完成动画?

时间:2013-11-02 15:29:36

标签: c# wpf animation

我已经在一个方法中启动了一个动画,之后我将不得不停止并立即完成它,如果它还没有完成的话。

Vector diff = ...
DoubleAnimation aniX = new DoubleAnimation(diff.X, TimeSpan.FromSeconds(0.4));
DoubleAnimation aniY = new DoubleAnimation(diff.Y, TimeSpan.FromSeconds(0.4));

aniXaniY如果尚未完成则停止。

1 个答案:

答案 0 :(得分:1)

我认为您应该使用XAML代码与一些附加的Trigger来阻止它。以编程方式停止动画。您可以尝试使用Begin开始StoryboardStop来停止它(不要使用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();

注意:上面的代码假设您在过程代码中声明了所有DoubleAnimationsStoryboard(不在XAML代码中)。我认为您应该在XAML代码中声明它们以使其更简洁,WPF中的许多类和内容都针对XAML进行了优化,所以有时您会觉得查看它们有点奇怪在程序代码中。