how to show label for couple of seconds in wpf?

烂漫一生 提交于 2019-12-24 10:39:25

问题


i want to show a label for a 3 seconds only and then disappear it. i am working in wpf application.

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

i started timer from the function

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;
      */
}

is this right way ? or is there any other easy way ?


回答1:


Set your Interval to 3000 and then just hide the label in the Tick event.




回答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>


来源:https://stackoverflow.com/questions/27648539/how-to-show-label-for-couple-of-seconds-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!