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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:05:51  来源:igfitidea点击:

How to stop an animation in C# / WPF?

提问by Daren Thomas

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)? 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...

现在,您将如何停止该动画 (the DoubleAnimation)?我想这样做的原因是因为我想开始新的动画(这似乎有效,但很难说)并最终停止最后一个动画......

采纳答案by TheSmurf

To stop it, call BeginAnimationagain with the second argument set to null.

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

回答by Brian Leahy

Place the animation in a StoryBoard. Call Begin() and Stop() on the storyboard to start to stop the animations.

将动画放置在 StoryBoard 中。在情节提要上调用 Begin() 和 Stop() 以开始停止动画。

回答by user3837

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**
)

回答by Nick

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
  • Remove the entire Storyboard
  • Remove the animation from the individual property

如果想让基值再次成为有效值,必须停止动画对属性的影响。使用故事板动画可以通过三种方式来做到这一点:

  • 将动画的 FillBehavior 属性设置为 Stop
  • 删除整个故事板
  • 从单个属性中删除动画

From MSDN

来自 MSDN

How to: Set a Property After Animating It with a Storyboard

如何:在使用故事板动画后设置属性

回答by Junior Mayhé

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!

我不喜欢它,但它在这里确实有效。试一试!

回答by BruceLH

There are two ways to stop a BeginAnimation. The first is to call BeginAnimation again with the second parameter set to null. This will remove all animations on the property and revertthe value back to its base value.

有两种方法可以停止 BeginAnimation。第一个是再次调用 BeginAnimation,并将第二个参数设置为 null。这将消除对财产的所有动画和还原值回其基本价值。

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. This will remove that specific animation and leave the value at its current position.

根据您使用该值的方式,这可能不是您想要的行为。第二种方法是将动画 BeginTime 设置为 null,然后用它调用 BeginAnimation。这将删除该特定动画并将值保留在其当前位置。

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);

回答by oliver

You can use this code:

您可以使用此代码:

[StoryBoardName].Remove([StoryBoardOwnerControl]);

回答by Fawaz

<Trigger.EnterActions>
       <BeginStoryboard x:Name="myStory">
       .........
       </BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
       <StopStoryboard BeginStoryboardName="myStory"/>
</Trigger.ExitActions>