【问题标题】:WPF - Animation to make an error message disappearWPF - 使错误消息消失的动画
【发布时间】:2011-01-12 20:29:41
【问题描述】:

我的窗口中有以下 xaml:

<Border Height="100" BorderBrush="Black" BorderThickness="2" CornerRadius="10" Background="PaleVioletRed" HorizontalAlignment="Center" VerticalAlignment="Center">
   <TextBlock Margin="10"  HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="17" FontWeight="Bold">Error Message Here</TextBlock>
</Border>

基本上显示这个:
alt text http://xs.to/thumb-4CB2_4B69F8E6.jpg

我计划将其 Visibility 绑定到错误状态变量,以便在发生错误时显示。

但我不想展示它很长时间。我希望它在 2 秒后消失/消失。有没有办法通过 XAML 做到这一点?还是一种不错的 WPF 方式?

类似这样的伪代码逻辑:

when (ErrorMessage.Visibility == Visible )
{
    Wait(2000); // Wait 2 seconds
    ErrorMessage.Visibility == Collapsed;
}

但最好使用 XAML 完成。

我的直觉告诉我有一种方法可以用动画来做到这一点,但我不是动画专家,需要一些帮助。

另一种选择是尝试设置一个计时器并用它来控制它。

【问题讨论】:

    标签: wpf animation


    【解决方案1】:

    使用类似的东西....

    <EventTrigger RoutedEvent="Page.Loaded">
    
        <BeginStoryboard>
             <Storyboard BeginTime="0:0:1">
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="image1" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" />
                       </ObjectAnimationUsingKeyFrames>
                  </Storyboard>
    
        </BeginStoryboard>
        </EventTrigger>
    

    更改路由事件以满足您的需求,将情节提要上的 BeginTime 设置为 2 分钟(或其他任何时间),将 targetname 设置为您的边框元素。

    【讨论】:

      猜你喜欢
      • 2019-06-02
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多