五秒后显示另一个窗口

时间:2013-01-21 08:02:22

标签: c# wpf timer

我想制作一个像这样的WPF图: 运行程序时,主窗口显示,五秒钟后,显示另一个窗口。 我怎样才能实现它?我见过计时器,但我不能这样做。

2 个答案:

答案 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线程上运行。