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

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

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

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

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();

注意:我的第二个几乎相同,除了 "timer.tick += (o, args)" 行。我从以下位置获得此代码:Here。这是一个表单应用程序代码,所以我只是尝试了那部分并且它有效。

我直接从here.

得到的第一个代码

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

有没有更好的方法来解决这个问题或解决这个问题? 我是 Visual Studio.

的新人

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

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");

        }

提前致谢。

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

这个有效:

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>

代码隐藏:

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

当您单击按钮时,这将显示一个 "Error" 标签 3 秒,标签将在 3 秒后隐藏。

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>

隐藏代码(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);
        }
    }
}

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

输出