我目前正在用 C# 开发一个应用程序,我在其中显示一个 MessageBox。如何在几秒钟后自动关闭消息框?
最佳答案
您需要创建自己的窗口,代码隐藏包含一个加载的处理程序和一个计时器处理程序,如下所示:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Timer t = new Timer();
t.Interval = 3000;
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
t.Start();
}
void t_Elapsed(object sender, ElapsedEventArgs e)
{
this.Dispatcher.Invoke(new Action(()=>
{
this.Close();
}),null);
}
然后您可以通过调用 ShowDialog() 使您的自定义消息框出现:
MyWindow w = new MyWindow();
w.ShowDialog();
关于c# - C# 自动关闭消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4362235/