WPF string animation using StringAnimationUsingKey

2019-08-12 06:22发布

I want to animate the text of a textbox using StringAnimationUsingKeyFrames and DiscreteStringKeyFrame

<TextBlock x:Name="txtStateRunning" HorizontalAlignment="Center" VerticalAlignment="Center">
   <TextBlock.Triggers>
         <EventTrigger RoutedEvent="TextBlock.Loaded">
             <BeginStoryboard>
                 <Storyboard x:Name="textAnimation">
                     <StringAnimationUsingKeyFrames Storyboard.TargetName="txtStateRunning" 
                                                           Storyboard.TargetProperty="Text" RepeatBehavior="Forever" AutoReverse="False" >
                         <DiscreteStringKeyFrame Value="Running ."   KeyTime="0:0:0" />
                         <DiscreteStringKeyFrame Value="Running .."  KeyTime="0:0:0.5" />
                         <DiscreteStringKeyFrame Value="Running ..." KeyTime="0:0:1" />
                         <DiscreteStringKeyFrame Value="Running ...." KeyTime="0:0:1.5" />
                     </StringAnimationUsingKeyFrames>
                 </Storyboard>
             </BeginStoryboard>
            </EventTrigger>
        </TextBlock.Triggers>
    </TextBlock>

In the code I have 4 * DiscreteStringKeyFrame but actually I see only 3 * running. The DiscreteStringKeyFrame at time 1.5 is not executed. What I am missing?

标签: wpf animation
1条回答
干净又极端
2楼-- · 2019-08-12 06:56

It is happening because your last frame begining to show and instantly replacing by first frame.

You can fix it by adding one more DiscreteStringKeyFrame at the end:

<DiscreteStringKeyFrame Value="" KeyTime="0:0:2" />

Or by setting Duration for StringAnimationUsingKeyFrames:

<StringAnimationUsingKeyFrames Duration="0:0:2" ...
查看更多
登录 后发表回答