简体   繁体   English

如何在 C#/WPF 中停止动画?

[英]How to stop an animation in C# / WPF?

I have something like this:我有这样的事情:

barProgress.BeginAnimation(RangeBase.ValueProperty, new DoubleAnimation(
    barProgress.Value, dNextProgressValue,
    new Duration(TimeSpan.FromSeconds(dDuration)));

Now, how would you stop that animation (the DoubleAnimation )?现在,您将如何停止该动画( DoubleAnimation )? The reason I want to do this, is because I would like to start new animations (this seems to work, but it's hard to tell) and eventually stop the last animation...我想这样做的原因是因为我想开始新的动画(这似乎有效,但很难说)并最终停止最后一个动画......

要停止它,请再次调用BeginAnimation并将第二个参数设置为null

When using storyboards to control an animation, make sure you set the second parameter to true in order to set the animation as controllable:使用故事板控制动画时,请确保将第二个参数设置为 true 以将动画设置为可控:

public void Begin(
    FrameworkContentElement containingObject,
    **bool isControllable**
)

There are two ways to stop a BeginAnimation.有两种方法可以停止一个 BeginAnimation。 The first is to call BeginAnimation again with the second parameter set to null.第一个是再次调用 BeginAnimation,并将第二个参数设置为 null。 This will remove all animations on the property and revert the value back to its base value.这将消除对财产的所有动画和还原值回其基本价值。

Depending on how you are using that value this may not be the behavior you want.根据您使用该值的方式,这可能不是您想要的行为。 The second way is to set the animations BeginTime to null then call BeginAnimation with it.第二种方法是将动画 BeginTime 设置为 null,然后用它调用 BeginAnimation。 This will remove that specific animation and leave the value at its current position.这将删除该特定动画并将值保留在其当前位置。

DoubleAnimation myAnimation = new Animation();
// Initialize animation
...

// To start
element.BeginAnimation(Property, myAnimation);

// To stop and keep the current value of the animated property
myAnimation.BeginTime = null;
element.BeginAnimation(Property, myAnimation);
<Trigger.EnterActions>
       <BeginStoryboard x:Name="myStory">
       .........
       </BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
       <StopStoryboard BeginStoryboardName="myStory"/>
</Trigger.ExitActions>

In my case I had to use two commands, my xaml has a button which fires a trigger, and its trigger fires the storyboard animation.在我的例子中,我不得不使用两个命令,我的 xaml 有一个触发触发器的按钮,它的触发器触发故事板动画。

I've put a button to stop animation with this code behind:我在后面放了一个按钮来停止动画:

MyBeginStoryboard.Storyboard.Begin(this, true);
MyBeginStoryboard.Storyboard.Stop(this);

I don't like it but it really works here.我不喜欢它,但它在这里确实有效。 Give it a try!试一试!

If you want the base value to become the effective value again, you must stop the animation from influencing the property.如果想让基值再次成为有效值,必须停止动画对属性的影响。 There are three ways to do this with storyboard animations:使用故事板动画可以通过三种方式来做到这一点:

  • Set the animation's FillBehavior property to Stop将动画的 FillBehavior 属性设置为 Stop
  • Remove the entire Storyboard删除整个故事板
  • Remove the animation from the individual property从单个属性中删除动画

From MSDN来自 MSDN

How to: Set a Property After Animating It with a Storyboard如何:在使用故事板动画后设置属性

Place the animation in a StoryBoard.将动画放置在 StoryBoard 中。 Call Begin() and Stop() on the storyboard to start to stop the animations.在情节提要上调用 Begin() 和 Stop() 以开始停止动画。

您可以使用此代码:

[StoryBoardName].Remove([StoryBoardOwnerControl]);

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

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