【问题标题】:How to change Text property of TextBlock in between animations?如何在动画之间更改 TextBlock 的 Text 属性?
【发布时间】:2017-10-06 11:24:30
【问题描述】:

我的 WPF 应用程序具有如下动画:

一个。将 TextBlock 从 000 旋转到 090。
乙。将 TextBlock 的 Text 属性更新为新值
c。继续将 TextBlock 从 090 旋转到 180。

我可以通过向 StoryBoard 的子项添加两个 DoubleAnimation 来实现步骤 a 和 c。有没有办法捕获第一个动画的结尾来做一些工作?

谢谢。

【问题讨论】:

    标签: wpf silverlight animation


    【解决方案1】:

    [适用于 .NET Framework 4.5 及更高版本]

    您可以使用 StringAnimationUsingKeyFrames 类使用 DiscreteStringKeyFrame 修改文本。以下是example

    <Button Name="myAnimatedButton" Margin="200"
          FontSize="16pt" FontFamily="Verdana">Some Text
          <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
              <BeginStoryboard>
                <Storyboard>
                  <StringAnimationUsingKeyFrames 
                    Storyboard.TargetName="myAnimatedButton" Storyboard.TargetProperty="(Button.Content)"
                    Duration="0:0:8" FillBehavior="HoldEnd">
    
                    <!-- All the key frames below are DiscreteStringKeyFrames. Discrete key frames create 
                    sudden "jumps" between values (no interpolation). Only discrete key frames can be used 
                    for String key frame animations. -->
                    <DiscreteStringKeyFrame Value="" KeyTime="0:0:0" />
                    <DiscreteStringKeyFrame Value="A" KeyTime="0:0:1" />
                    <DiscreteStringKeyFrame Value="An" KeyTime="0:0:1.5" />
                    <DiscreteStringKeyFrame Value="Ani" KeyTime="0:0:2" />
                    <DiscreteStringKeyFrame Value="Anim" KeyTime="0:0:2.5" />
                    <DiscreteStringKeyFrame Value="Anima" KeyTime="0:0:3" />
                    <DiscreteStringKeyFrame Value="Animat" KeyTime="0:0:3.5" />
                    <DiscreteStringKeyFrame Value="Animate" KeyTime="0:0:4" />
                    <DiscreteStringKeyFrame Value="Animated" KeyTime="0:0:4.5" />
                    <DiscreteStringKeyFrame Value="Animated " KeyTime="0:0:5" />
                    <DiscreteStringKeyFrame Value="Animated T" KeyTime="0:0:5.5" />
                    <DiscreteStringKeyFrame Value="Animated Te" KeyTime="0:0:6" />
                    <DiscreteStringKeyFrame Value="Animated Tex" KeyTime="0:0:6.5" />
                    <DiscreteStringKeyFrame Value="Animated Text" KeyTime="0:0:7" />
                  </StringAnimationUsingKeyFrames>              
                </Storyboard>
              </BeginStoryboard>
            </EventTrigger> 
          </Button.Triggers>
        </Button>
    

    感谢 .NET 框架中动画集合的最新添加。

    【讨论】:

      【解决方案2】:

      您可以添加一个 BooleanAnimationUsingKeyFrames 并使用它来设置值。

      http://msdn.microsoft.com/en-us/library/ms745819.aspx

      【讨论】:

        【解决方案3】:

        您可以创建两个故事板,一个用于旋转到 90,另一个旋转到 180。当第一个故事板完成时,更新文本,然后开始下一个故事板。

        Storyboard rotateTo90 = new Storyboard();
        // Add rotate animation
        rotateTo90.Completed += (s,e) => 
            { 
                 TextBlock1.Text = "Updated";
                 Storyboard rotateTo180 = new Storyboard();
                 // Add rotate animation
                 rotateTo180.Begin();
            };
        rotateTo90.Begin();
        

        【讨论】:

        • 谢谢。正是我要找的!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-13
        • 1970-01-01
        相关资源
        最近更新 更多