WPF_Storyboard实现文字消息弹窗动画

本文展示了如何在WPF应用中利用Storyboard实现文字消息弹窗的动画效果,包括XAML和C#代码的详细说明,帮助开发者创建带有动态点点效果的文本通知。

*.先上效果图(文本后面带有点点动画的效果)

xaml中相关代码如下:

<TextBlock x:Name="tBlockStatus" FontSize="12" HorizontalAlignment="Center" Foreground="#999999">
                    <TextBlock.Resources>
                        <Storyboard x:Key="TextWaitingStoryboard" RepeatBehavior="Forever" Duration="0:0:3.5">
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="waitText0">
                                <DiscreteColorKeyFrame KeyTime="0:0:1.0" Value="#999999"/>
                            </ColorAnimationUsingKeyFrames>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="waitText1">
                                <DiscreteColorKeyFrame KeyTime="0:0:1.5" Value="#999999"/>
                            </ColorAnimationUsingKeyFrames>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="waitText2">
                                <DiscreteColorKeyFrame KeyTime="0:0:2.5" Value="#999999"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </TextBlock.Resources>
                     <Run>正在等待连接</Run>
                     <Run x:Name="waitText0" Foreground="#00999999">.</Run>
                     <Run x:Name="waitText1" Foreground="#00999999">.</Run>
                     <Run x:Name="waitText2" Foreground="#00999999">.</Run>
                </TextBlock>

cs中代码控制动画开始和停止:

    this.textWaitingStoryboard = (Storyboard)this.tBlockStatus.FindResource("TextWaitingStoryboard");

     this.textWaitingStoryboard.Begin(); //动画开始
     this.textWaitingStoryboard.Stop();  //动画停止

*.效果图如下:

xaml中代码如下:

 <Border Name="TipMessageBorder" Height="40" Margin="24,0" Background="#FFF2F0" BorderBrush="#FFCCC7" BorderThickness="1" CornerRadius="4" Visibility="Hidden">
                <Border.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform CenterX="100" CenterY="20"></ScaleTransform>
                    </TransformGroup>
                </Border.RenderTransform>
                <Border.Resources>
                    <Storyboard x:Key="TipMessageStoryboard">
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="Visibility">
                            <ObjectAnimationUsingKeyFrames.KeyFrames>
                                <DiscreteObjectKeyFrame KeyTime="0:0:0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <Visibility>Visible</Visibility>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                                <DiscreteObjectKeyFrame KeyTime="0:0:3">
                                    <DiscreteObjectKeyFrame.Value>
                                        <Visibility>Hidden</Visibility>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames.KeyFrames>
                        </ObjectAnimationUsingKeyFrames>
                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX" From="0.2" To="0.5" Duration="0:0:0.04"/>
                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY" From="0.2" To="0.5" Duration="0:0:0.04"/>
                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX" From="0.5" To="1" Duration="0:0:0.04" BeginTime="0:0:0.04"/>
                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY" From="0.5" To="1" Duration="0:0:0.04" BeginTime="0:0:0.04"/>
                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX" From="1" To="1.2" Duration="0:0:0.04" BeginTime="0:0:0.08" AutoReverse="True"/>
                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY" From="1"  To="1.2" Duration="0:0:0.04" BeginTime="0:0:0.08" AutoReverse="True"/>

                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX" From="1" To="1.2" Duration="0:0:0.04"  AutoReverse="True" BeginTime="0:0:2.92"/>
                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY" From="1" To="1.2" Duration="0:0:0.04"  AutoReverse="True" BeginTime="0:0:2.92"/>
                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX" From="1" To="0.5" Duration="0:0:0.04" BeginTime="0:0:2.96"/>
                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY" From="1" To="0.5" Duration="0:0:0.04" BeginTime="0:0:2.96"/>
                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX" From="0.5" To="0.2" Duration="0:0:0.04" BeginTime="0:0:2.96"/>
                        <DoubleAnimation Storyboard.TargetName="TipMessageBorder" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY" From="0.5"  To="0.2" Duration="0:0:0.04" BeginTime="0:0:2.96"/>
                    </Storyboard>
                </Border.Resources>
                <StackPanel Orientation="Horizontal">
                    <Image Source="/WeDou.PCClient;component/Resources/Image/failed.png" Stretch="None" Margin="10,0" />
                    <TextBlock Text="{Binding TipMessage}" VerticalAlignment="Center" FontSize="14" Foreground="#F76B69"/>
                </StackPanel>
            </Border>

cs代码如下:

  private Storyboard storyboard;


  this.storyboard =(Storyboard)this.TipMessageBorder.FindResource("TipMessageStoryboard");

  public void ShowTipMessage()
   {
      this.storyboard.Begin();
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zxy644492473

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值