WPF动画无法正常工作

时间:2017-09-22 20:22:51

标签: wpf xaml

我的Image控件有以下故事板,但是,在运行此代码时,最后一个图像不会显示:

<Image Height="15" Width="137" RenderTransformOrigin="0.415,4.583" Canvas.Left="104" Canvas.Top="13">

    <Image.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding State, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="Reconnect">
                    <DataTrigger.EnterActions>                  
                        <BeginStoryboard Name="reconnectStoryBoardImageChange" >
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames 
                                            BeginTime="00:00:00" RepeatBehavior="Forever"
                                            Storyboard.TargetProperty="(Image.Source)">
                                    <DiscreteObjectKeyFrame KeyTime="00:00:01">
                                        <DiscreteObjectKeyFrame.Value>
                                            <BitmapImage UriSource="Images/State/Reconnect.png" />
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:02">
                                        <DiscreteObjectKeyFrame.Value>
                                            <BitmapImage UriSource="Images/State/Reconnect_2.png" />
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:03">
                                        <DiscreteObjectKeyFrame.Value>
                                            <BitmapImage UriSource="Images/State/Reconnect_3.png" />
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:04">
                                        <DiscreteObjectKeyFrame.Value>
                                            <BitmapImage UriSource="Images/State/Reconnect_4.png" />
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>                       
                        <StopStoryboard BeginStoryboardName="reconnectStoryBoardImageChange"/>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

XAML在不同的时间显示不同的图像,但从不显示reconnect_4,直到_3然后再回到第一张图像。

我做错了吗?

1 个答案:

答案 0 :(得分:0)

你可以找到解释为什么它没有按照你期望的方式工作here

  

如果动画的持续时间为自动或其持续时间等于   最后一个关键帧的时间,动画结束。否则,如果   动画的持续时间大于最后一个键的关键时间   帧,动画保持关键帧值,直到它到达   持续时间结束。

因此,您应指定Duration的{​​{1}}。我会这样做:

Storyboard

我指定{4}作为<Style.Triggers> <DataTrigger Binding="{Binding State, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="Reconnect"> <DataTrigger.EnterActions> <BeginStoryboard Name="reconnectStoryBoardImageChange" > <Storyboard> <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:04" RepeatBehavior="Forever" Storyboard.TargetProperty="(Image.Source)"> <DiscreteObjectKeyFrame KeyTime="00:00:00"> <DiscreteObjectKeyFrame.Value> <BitmapImage UriSource="Images/State/Reconnect.png" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> <DiscreteObjectKeyFrame KeyTime="00:00:01"> <DiscreteObjectKeyFrame.Value> <BitmapImage UriSource="Images/State/Reconnect_2.png" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> <DiscreteObjectKeyFrame KeyTime="00:00:02"> <DiscreteObjectKeyFrame.Value> <BitmapImage UriSource="Images/State/Reconnect_3.png" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> <DiscreteObjectKeyFrame KeyTime="00:00:03"> <DiscreteObjectKeyFrame.Value> <BitmapImage UriSource="Images/State/Reconnect_4.png" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </DataTrigger.EnterActions> <DataTrigger.ExitActions> <StopStoryboard BeginStoryboardName="reconnectStoryBoardImageChange"/> </DataTrigger.ExitActions> </DataTrigger> </Style.Triggers> 。第一张图像显示在00:00:00到00:00:01。第二张图像显示在00:00:01到00:00:02。第三张图像显示在00:00:02到00:00:03。第四张图片显示在00:00:03至00:00:04。然后它重新启动。

在你的情况下,最后一个关键帧持续0秒,因为你的故事板结束得太早。