WPF 动画只触发一次 [英] WPF Animation Only Firing Once

查看:28
本文介绍了WPF 动画只触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小椭圆,每次依赖属性设置为 true 时,我都想闪烁.因为该属性可以在几毫秒内快速从 true 变回 false,所以我需要使用动画而不是简单的样式数据触发器来执行此操作.基本上,我只想要在椭圆上 ping 动画的真实值.

I have a small ellipse that I want to flash every time a dependency property gets set to true. Because the property can very quickly go from true back to false in a matter of milliseconds, I need to do this with an animation and not a simple style datatrigger. Basically, I just want the true value to ping an animation on the ellipse.

<Ellipse Height="10" Width="10" Stroke="#FFFFFFFF" Margin="5,3,0,0">
    <Ellipse.Fill>
        <SolidColorBrush />
    </Ellipse.Fill>
    <Ellipse.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsReceiving}" Value="True" >
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Fill.Color">
                                    <ColorAnimationUsingKeyFrames.KeyFrames>
                                        <DiscreteColorKeyFrame KeyTime="0:0:0" Value="Red"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:0.25" Value="Transparent"/>
                                    </ColorAnimationUsingKeyFrames.KeyFrames>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Ellipse.Style>
</Ellipse>

这个动画似乎有效,但它只在值第一次达到真时触发.我错过了什么吗?

This animation seems to work, but it only fires the first time the value reaches true. Am I missing something?

更新:感谢大家的投入.事实证明,这是一个线程问题.最初,我在绑定到实现 INotifyPropertyChanged 的​​视图模型的控件上有一个 DP.然后我尝试删除控件上的 DP 并将我的视图模型属性转换为 DP.Boom,那时我开始收到一个错误,指出另一个线程拥有该对象.我意识到我需要像我在应用程序的其他部分所做的那样使用 Reactive Extensions 合并一些 Observables.我使用 PropertyChanged() 恢复到视图模型的传统属性,并简单地将其绑定到控件的动画.现在一切正常.

UPDATE: Thanks for the input everybody. It turns out, it was a threading issue. Originally, I had a DP on the control that was bound to a view model that implemented INotifyPropertyChanged. I then tried removing the DP on the control and turning my view model property into a DP. Boom, that's when I started getting an error stating that a different thread owned the object. I realized I needed to incorporate some Observables using Reactive Extensions as I had done in other parts of the app. I reverted back to the view model traditional property with PropertyChanged() and simply bound that to the control's animation. Everything is working flawlessly now.

推荐答案

我遇到了类似的问题,当 DP 更改时,我有一个in"和out"动画要触发.第一个周期运行良好,但之后什么也没发生.我通过在 BeginStoryboard 操作之前添加 RemoveStoryboard EnterActions 解决了这个问题.

I had a similar problem where I had an 'in' and 'out' animation to be fired when a DP changed. The first cycle worked fine but after that nothing happened. I solved it by adding RemoveStoryboard EnterActions before the BeginStoryboard actions.

<Border Name="Zyzzyx" Grid.Row="1" Background="DarkRed" Height="0" VerticalAlignment="Bottom">
    <Border.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=SelectionState, ElementName=ControlRoot, Mode=OneWay}" Value="Selecting">
                    <DataTrigger.EnterActions>
                        <RemoveStoryboard BeginStoryboardName="AnimateIn" />
                        <RemoveStoryboard BeginStoryboardName="AnimateOut" />
                        <BeginStoryboard Name="AnimateIn" HandoffBehavior="SnapshotAndReplace">
                            <Storyboard FillBehavior="HoldEnd" Duration="0:0:5">
                                <DoubleAnimation To="30" Duration="0:0:5" Storyboard.TargetProperty="Height" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=SelectionState, ElementName=ControlRoot, Mode=OneWay}" Value="Deselecting">
                    <DataTrigger.EnterActions>
                        <RemoveStoryboard BeginStoryboardName="AnimateIn" />
                        <RemoveStoryboard BeginStoryboardName="AnimateOut" />
                        <BeginStoryboard Name="AnimateOut" HandoffBehavior="SnapshotAndReplace">
                            <Storyboard FillBehavior="HoldEnd" Duration="0:0:5">
                                <DoubleAnimation To="0" Duration="0:0:5" Storyboard.TargetProperty="Height" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>

<!-- Border content -->

</Border>

这篇关于WPF 动画只触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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