在 wpf C# 中显示标签 3/5 秒,但单击后它不会停留那么长时间

1 c# wpf visual-studio dispatchertimer visual-studio-2015

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

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

   //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;
}
Run Code Online (Sandbox Code Playgroud)

   //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();
Run Code Online (Sandbox Code Playgroud)

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

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

问题是这在第一次和第二次都很好用。也许第三。但在那之后,我觉得计时器秒数正在减少。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");

        }
Run Code Online (Sandbox Code Playgroud)

提前致谢。

goo*_*ing 6

这有效:

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>
Run Code Online (Sandbox Code Playgroud)

代码隐藏:

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;
}
Run Code Online (Sandbox Code Playgroud)


Dee*_*ath 5

当您单击按钮时,这将显示“错误”标签 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>
Run Code Online (Sandbox Code Playgroud)

代码隐藏(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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

输出 在此处输入图片说明