这是预期的行为吗?如果我有一个在屏幕上设置动画的标签,并且我将其删除,那么即使我没有引用该标签或我用来为其设置动画的标签canvas1.Children.Remove(label)
,动画似乎仍会继续在幕后运行甚至触发。如果动画是无止境的呢?AnimationCompleted
DoubleAnimation
从父级中删除控件时,我真的需要自己记账并自己停止动画吗?
我真的需要自己记账吗
是的,您只能使用Animate
Dependency
属性和Dependency
属性static
,即使您Control
不再被引用,Animation
也将继续。
根据您的目的,Animation
可能就像将动画设置为一样FillBehavior
简单Stop
animation.FillBehavior = FillBehavior.Stop;
这将在动画TimeLine
完成时停止动画,但如果您有重复动画或设置某些值的动画,这并不总是一个很好的解决方案,因为当动画停止时,动画属性将立即返回其原始值。
如果FillBehavior.Stop
在您的情况下不起作用,您可以调用停止Animations
和Storyboards
其他方式。
如果Animation
是Storyboard
你可以打电话StopStoryboard
当您使用时,BeginStoryboard
您可以提供一个Name
,然后您可以使用它StopStoryboard
:
<BeginStoryboard Name="myStoryboard" ......... />
<StopStoryboard BeginStoryboardName="myStoryboard" />
触发情节提要示例:
<Trigger>
<Trigger.EnterActions>
<BeginStoryboard Name="myStoryboard">
<Storyboard ..... />
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<StopStoryboard BeginStoryboardName="myStoryboard" />
</Trigger.ExitActions>
</Trigger>
或者在你背后的代码中,你可以调用Stop
你的故事板:
myStoryboard.Stop();
如果您在后面的代码中创建一个简单的动画,您可以通过将 anull
TimeLine
应用于DependencyProperty
您的动画来停止它。
例子:
// Start
myLabel.BeginAnimation(Canvas.LeftProperty, new DoubleAnimation(10.0, 100.0, new Duration(TimeSpan.FromSeconds(5)));
// Stop
myLabel.BeginAnimation(Canvas.LeftProperty, null);