如何查看边框/弹出窗口5秒钟?

时间:2013-04-25 07:45:47

标签: c# wpf animation styles storyboard

我想设置一个像popup一样的边框,这个边框会在5秒后显示然后崩溃。当边框可见时,它将仅在5秒内可见,然后折叠。每次边框可见时都会重复此操作。我试过但它只出现过一次。有人可以向我解释我犯的错误吗?

这是我实施的风格

<Grid.Resources>
        <Style x:Key="NotificationStyle" TargetType="{x:Type Border}">
            <Style.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard x:Name="NotificationStoryBoard">
                            <Storyboard >
                                <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.Visibility)">
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>

                                    <DiscreteObjectKeyFrame KeyTime="00:00:05" Value="{x:Static Visibility.Collapsed}"/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>

这是边境......

<Border Name="NotifciationPopUp" Background="White" Height="80" Width="200" BorderBrush="Gray" BorderThickness="1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Visibility="Collapsed"  Margin="10,0,0,10" Style="{StaticResource NotificationStyle}">

1 个答案:

答案 0 :(得分:0)

我意识到以这种方式隐藏了边界:

风格:

<Style x:Key="NotificationStyle" TargetType="{x:Type Border}">
    <Style.Triggers>
        <Trigger Property="Visibility" Value="Visible">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" BeginTime="0:0:0" From="0.0" To="1.0" Duration="0:0:1.0" />
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" BeginTime="0:0:5" From="1.0" To="0.0" Duration="0:0:1.0" />
                    </Storyboard>
                </BeginStoryboard>
           </Trigger.EnterActions>
       </Trigger>
   </Style.Triggers>
</Style>

网格:

<Grid>
    <Border Name="MyBorder" Width="100" Visibility="Hidden" Height="50" BorderThickness="2" BorderBrush="OrangeRed" Style="{StaticResource NotificationStyle}">
        <TextBlock Text="Some information" TextAlignment="Center" VerticalAlignment="Center" />
    </Border>

    <ToggleButton Name="MyButton" Content="Show border" Width="100" Height="30" VerticalAlignment="Bottom" Click="Button_Click" />
</Grid>

在代码中:

private void Button_Click(object sender, RoutedEventArgs e)
{            
    if (MyButton.IsChecked == true) 
    {
        MyBorder.Visibility = System.Windows.Visibility.Visible;
    }

    if (MyButton.IsChecked == false)
    {
        MyBorder.Visibility = System.Windows.Visibility.Hidden;
    } 
}

从他的实验中我了解到,如果该属性曾经用于动画,那么只能使用动画触发器来处理它。因此,我没有收到财产的访问代码。

此外,然后控件使用Visibility = Collapsed,他没有从系统接收消息,动画也不起作用。虽然属性“隐藏”应该接收消息,但我没有像动画一样好,所以在示例中我使用不透明度