我想制作一个像这样的WPF图: 运行程序时,主窗口显示,五秒钟后,显示另一个窗口。 我怎样才能实现它?我见过计时器,但我不能这样做。
答案 0 :(得分:4)
感谢@Kshitij Mehta提供有关DispatcherTimer的指针。
在MainWindow
中,定义一个DispatcherTimer并在Tick上弹出另一个窗口 -
DispatcherTimer timer = null;
void StartTimer()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(5);
timer.Tick += new EventHandler(timer_Elapsed);
timer.Start();
}
void timer_Elapsed(object sender, EventArgs e)
{
timer.Stop();
AnotherWindow window = new AnotherWindow();
window.Show();
}
在StartTimer()
构造函数中调用MainWindow
。
public MainWindow
{
InitializeComponent();
StartTimer();
}
答案 1 :(得分:3)
您想要使用DispatcherTimer。 Prateek Singh几乎做对了。我只是将他的Timer更改为DispatcherTimer,因此它在UI线程上运行。