如何以示对夫妇在WPF秒标签?(how to show label for couple of se

2019-10-21 12:15发布

我想显示只有3秒的标签,然后消失了。 我在WPF应用程序的工作。

public DispatcherTimer timer = new DispatcherTimer(); 
timer.Tick += new EventHandler(timer_Tick);

我开始计时从功能

timer.Start(); 

private void timer_Tick(object sender, EventArgs e)
{
      /*
      if timer equals 3 seconds then 
      timer.stop();
      lblToast.Visibility = Visibility.Hidden;
      else
      lblToast.Visibility = Visibility.Visible;
      */
}

这是正确的方式? 或是否有任何其他简单的方法?

Answer 1:

您所设定的Interval到3000,然后就隐藏在标签Tick事件。



Answer 2:

Using Wpf animation you can do this very easily.For animation visit this link

   <Label Content="Hello World">
        <Label.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames BeginTime="0:0:0"  Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0:0:3" Value="{x:Static Visibility.Collapsed}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Label.Triggers>
    </Label>


文章来源: how to show label for couple of seconds in wpf?