在 wpf C# 中显示 3/5 秒的标签,但单击几下后它不会停留那么久

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30582613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:10:43  来源:igfitidea点击:

Showing a label for 3/5 seconds in wpf C# but after some click it doesn't stay that longer

c#wpfvisual-studiovisual-studio-2015dispatchertimer

提问by Deepu Reghunath

I'm trying to show text in a label in wpf when a button is pressed and then hide after couple of seconds. I know there are answers of this, but my problem is different.

我试图在按下按钮时在 wpf 的标签中显示文本,然后在几秒钟后隐藏。我知道这有答案,但我的问题是不同的。

I used these 2 ways for hiding the label:

我使用了这两种方法来隐藏标签:

One

   //When the button is pressed
   label_plus.Visibility = System.Windows.Visibility.Visible;

   DispatcherTimer timer = new DispatcherTimer();
   timer.Interval = new TimeSpan(0, 0, 5);
   timer.Tick += timer_Tick;                //Or, timer.Tick += new EventHandler(timer_Tick);
   timer.Start();

// The timer event handler
void timer_Tick(object sender, EventArgs e)
        {
            label_plus.Visibility = System.Windows.Visibility.Collapsed;
}

Two

   //Button pressed
   label_plus.Content = label_plus1.Content = "+";
   DispatcherTimer timer = new DispatcherTimer();
   timer.Interval = new TimeSpan(0, 0, 5);
   timer.Tick += (o, args) => label_plus.Content = "";
   timer.Start();

Note: I the second one is almost same, except the "timer.tick += (o, args)" line. I got this code from: Here. It was a Form application code, so I just tried that part and it worked.

注意:我的第二个几乎相同,除了“timer.tick += (o, args)”这一行。我从这里得到了这个代码:这里。这是一个表单应用程序代码,所以我只是尝试了那部分并且它起作用了。

The 1st code I got directly from here.

我直接从这里得到的第一个代码

The problem is this both works pretty well on 1st and second time.And maybe 3rd. But after that I feel like the timer second is decreasing. After 2/3 times, it hides within 3/4 seconds, after that it barely stays for 1 second or less.

问题是这在第一次和第二次都很好用。也许第三。但在那之后,我觉得计时器秒数正在减少。2/3 次后,它会在 3/4 秒内隐藏,之后几乎不会停留 1 秒或更短时间。

Is there a better way to do this or getting rid of this problem? I'm new in Visual Studio.

有没有更好的方法来做到这一点或摆脱这个问题?我是 Visual Studio 的新手。

Update:This also works well, but keeps repeating. Any way to stop after one process?

更新:这也很有效,但会不断重复。有什么办法可以在一个过程后停止?

var timer = new System.Timers.Timer();

            timer.Elapsed += timer_Tick;
            timer.Interval = 3000;
            timer.Enabled = true;
            timer.Start();

void timer_Tick(object sender, EventArgs e)
        {
            //label_plus.Visibility = label_plus1.Visibility = System.Windows.Visibility.Collapsed; 
            MessageBox.Show("Show some data");

        }

Thanks in advance.

提前致谢。

回答by goobering

This works:

这有效:

XAML:

XAML:

<StackPanel>
    <Button Width="50"
            Height="50"
            Click="Button_Click"
            Content="OK" />
    <Label x:Name="MyLabel"
            Content="THIS IS A LABEL"
            FontSize="30"
            Visibility="Collapsed" />
</StackPanel>

Codebehind:

代码隐藏:

private DispatcherTimer dispatcherTimer;

public MainWindow()
{
    InitializeComponent();

    //Create a timer with interval of 2 secs
    dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    //Things which happen before the timer starts
    MyLabel.Visibility = System.Windows.Visibility.Visible;

    //Start the timer
    dispatcherTimer.Start(); 
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    //Things which happen after 1 timer interval
    MessageBox.Show("Show some data");
    MyLabel.Visibility = System.Windows.Visibility.Collapsed;

    //Disable the timer
    dispatcherTimer.IsEnabled = false;
}

回答by Deepu Reghunath

This will shows an "Error" label for 3 sec when you click on the button and the label will gets hide after 3 sec.

当您单击按钮时,这将显示“错误”标签 3 秒,标签将在 3 秒后隐藏。

XAML

XAML

<Window.Resources>
    <Storyboard x:Key="sbHideAnimation" >
        <DoubleAnimation Storyboard.TargetProperty="Opacity"  From="1" To="1" Duration="0:0:3" /><!--label shows for 3 sec-->
        <DoubleAnimation Storyboard.TargetProperty="Opacity" BeginTime="0:0:3" From="1" To="0" DecelerationRatio=".5" Duration="0:0:2" /><!--Fade out the label after 3 sec-->
    </Storyboard>
</Window.Resources>
<Grid>
    <Label x:Name="lblError" Content="Error" FontSize="20" HorizontalAlignment="Center" Opacity="0"/>
    <Button Click="Button_Click" Content="Button" VerticalAlignment="Center" Width="100" />
</Grid>

Code behind(c#)

代码隐藏(c#)

using System.Windows;
using System.Windows.Media.Animation;

namespace WpfApplication1
{    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {           
            Storyboard sb = Resources["sbHideAnimation"] as Storyboard;
            sb.Begin(lblError);
        }
    }
}

You can adjust the time duration for showing the label by changing the value of attributes 'BeginTime' and 'Duration' in the Storyboard "sbHideAnimation"

您可以通过更改 Storyboard“sbHideAnimation”中的属性“BeginTime”和“Duration”的值来调整显示标签的持续时间

Outputenter image description here

输出在此处输入图片说明

回答by BlindGarret

You could always approach it as a WPF animation. They tend to give better control over things like fading in and out as needed. There are a lot of different approaches to this problem, but S.L. gives a couple of good visibility animation examples here: Apply animation on WPF control visibility change

您始终可以将其视为 WPF 动画。他们倾向于更好地控制诸如根据需要淡入淡出之类的事情。有很多不同的方法可以解决这个问题,但是 SL 在这里给出了几个很好的可见性动画示例:Apply animation on WPF control visible change