C# 在wpf C中显示标签3/5秒,但单击一段时间后,标签不会显示';别再呆那么久了

C# 在wpf C中显示标签3/5秒,但单击一段时间后,标签不会显示';别再呆那么久了,c#,wpf,visual-studio,visual-studio-2015,dispatchertimer,C#,Wpf,Visual Studio,Visual Studio 2015,Dispatchertimer,我试图在按下按钮时在wpf中显示标签中的文本,然后在几秒钟后隐藏。我知道这有答案,但我的问题不同 我使用以下两种方法隐藏标签: 一个 两个 注意:I第二行几乎相同,除了“timer.tick+=(o,args)”行。我从以下位置获得此代码:。这是一个表单应用程序代码,所以我只是尝试了这一部分,它起了作用 第一个代码我直接从 问题是,这两种方法在第一次和第二次都很有效。也许第三次也可以。但在那之后,我感觉计时器秒在减少在2/3次之后,它会在3/4秒内隐藏,之后它几乎不会停留1秒或更短时间。 有没有

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

我使用以下两种方法隐藏标签:

一个

两个

注意:I第二行几乎相同,除了“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");

        }

提前感谢。

您可以将其作为WPF动画处理。它们倾向于更好地控制像根据需要淡入淡出这样的事情。有很多不同的方法来解决这个问题,但是S.L.在这里给出了几个很好的可见性动画示例:

您可以将其作为WPF动画来处理。它们倾向于更好地控制像根据需要淡入淡出这样的事情。有很多不同的方法可以解决这个问题,但是S.L.在这里给出了几个很好的可见性动画示例:

这很有效:

XAML:

这项工作:

XAML:

当您单击按钮时,这将显示一个“错误”标签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>
您可以通过更改情节提要“sbHideAnimation”中属性“BeginTime”和“duration”的值来调整显示标签的持续时间

输出

当您单击按钮时,这将显示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>
您可以通过更改情节提要“sbHideAnimation”中属性“BeginTime”和“duration”的值来调整显示标签的持续时间

输出

谢谢,但我认为编程方法对我来说会容易得多。正如我所知,我不知道XAML中的高级代码。我用System.Timers.Timer尝试了另一个,做同样的工作。但问题是它会重复。有没有办法停止重复?注意:动画并不重要。显示/隐藏就足够了。谢谢,但我认为编程方法对我来说会容易得多。正如我所知,我不知道XAML中的高级代码。我用System.Timers.Timer尝试了另一个,做同样的工作。但问题是它会重复。有没有办法停止重复?注意:动画并不重要。显示/隐藏就足够了。正如您所观察到的,问题在于计时器是周期性的。尝试
((计时器)发送器).Enabled=false。另外,尝试只创建一个计时器并保存它,而不是每次需要短时间显示时都创建新的计时器。谢谢,它也有效。正如您所观察到的,问题是计时器是周期性的。尝试
((计时器)发送器).Enabled=false。另外,尝试只创建一个计时器并保存它,而不是每次需要短时间显示时都创建新的计时器。谢谢,它也起作用了。谢谢,它起作用了。而且解释也很有帮助。顺便说一句,我必须把“private dispatchermer dispatchermer;”放在主窗口之后,否则它就不起作用了。我在类声明(public partial class MainWindow:window)之后有这一行,但在上面显示的构造函数之前。它应该是一个全局变量,所以你不能把它放在一个方法里面。谢谢,它成功了。而且解释也很有帮助。顺便说一句,我必须把“private dispatchermer dispatchermer;”放在主窗口之后,否则它就不起作用了。我在类声明(public partial class MainWindow:window)之后有这一行,但在上面显示的构造函数之前。它应该是一个全局变量,所以不能放在方法中。
<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;
}
<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>
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);
        }
    }
}