停止动画超出范围 [英] Stop animation outside of scope

查看:56
本文介绍了停止动画超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的动画,该动画对视图进行了一些重大更改(更改了某些控件的可见性,不透明度,背景画笔等),并且我想还原该动画的功能.停止/删除情节提要应该"做到这一点.

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.

System.Windows.Media.Animation警告:6:无法执行操作,因为指定的Storyboard从未应用于此对象进行交互式控制.动作=删除" ......

System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='Remove' ......

这是我的做法:

<!-- 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>

这篇关于停止动画超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆