问题描述
我有一个复杂的动画,该动画对视图进行了一些重大更改(更改了某些控件的可见性,不透明度,背景画笔等),并且我想还原该动画的功能.停止/删除情节提要应该"做到这一点.
I have a complicated animation, which does some heavy changes to a view (changes visibility, opacity, background brushes, etc. of some controls) and I'd like to revert what this animation did. Stop/Remove storyboard "should" do that.
但是,有一个问题:
单击一个按钮时动画运行,但是单击另一个按钮时动画停止.通过这种方法,我得到了以下错误.
The animation runs when one button is clicked, but stopped when another is clicked. And with this approach I am getting the following error.
这是我的做法:
<!-- button which start animation -->
<Button ...>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard x:Name="storyboardUserClick">
<Storyboard>
<ObjectAnimationUsingKeyFrames ...
<!-- button which should revert what animation did --->
<Button ...>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<RemoveStoryboard BeginStoryboardName="storyboardUserClick" ...
是否有一种简单的方法来实现该目标,最好不要添加代码(在最坏的情况下,附加属性可能是一种选择)?我觉得这很简单...
Is there a simple way, preferably without code behind (well, attached property may be an option in worst case) to achieve that? I have feeling it is something very simple...
推荐答案
我找到了答案此处.
这个想法是将动画(我说动画,但我指的是 Storyboard
,更确切地说是 BeginStoryboard
)移到可由 RemoveStoryboard
.像
The idea is to move animation (I say animation, but I mean Storyboard
and even more specifically BeginStoryboard
) into a scope accessible by RemoveStoryboard
. Something like
<Grid>
<Button x:Name="buttonStart" ...>
<Button x:Name="buttonStop" ...>
<Grid.Triggers>
<EventTrigger SourceName="buttonStart" RoutedEvent="Button.Click">
<BeginStoryboard x:Name="storyboardUserClick">
<Storyboard>
<ObjectAnimationUsingKeyFrames ...
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger SourceName="buttonStop" RoutedEvent="Button.Click">
<RemoveStoryboard BeginStoryboardName="storyboardUserClick" />
</EventTrigger>
</Grid.Triggers>
</Grid>
这篇关于停止动画超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!