1

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

4

2 回答 2

5

感谢@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();
    }
于 2013-01-21T08:09:58.920 回答
4

您想为此使用DispatcherTimer。Prateek Singh 几乎做对了。我只是将他的 Timer 更改为 DispatcherTimer 以便它在 UI 线程上运行。

于 2013-01-21T08:15:21.173 回答