2

我正在创建 WPF 项目,我有两个屏幕

1)载入画面

2)主窗口

我的代码如下加载屏幕

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        loadingThread = new Thread(load);
        loadingThread.SetApartmentState(ApartmentState.STA);
        loadingThread.Start();

    }

   private void load()
    {
        Thread.Sleep(1000);
        this.Dispatcher.Invoke(showDelegate, "Loading UI...");
        Thread.Sleep(2000);
        //do some loading work
        this.Dispatcher.Invoke(hideDelegate);

        Thread.Sleep(2000);
        this.Dispatcher.Invoke(DispatcherPriority.Normal,
        (Action)delegate() { this.CLose(); });

        MainWindow Mw = new MainWindow();//Gives me the error
        Mw.ShowDialog();
    }

当我调用我的主窗口屏幕时,我收到错误“应用程序对象正在关闭”。我相信,因为线程正在关闭,我收到了错误,谁能告诉我调用我的主窗口的其他可能方法是什么

谢谢你们

4

1 回答 1

1

您正在关闭调用应用程序关闭命令的最后一个窗口。在关闭“此”窗口之前创建并打开新窗口:
您可以更改此:

this.Dispatcher.Invoke(DispatcherPriority.Normal,
    (Action)delegate() { this.Close(); });

对此:

this.Dispatcher.Invoke(DispatcherPriority.Normal,
(Action)delegate() 
{
    MainWindow Mw = new MainWindow();
    // Mw.ShowDialog(); I changed this line because it cannot be a dialog here.
    Mw.Show();
    this.Close();
});
于 2013-05-23T06:55:39.150 回答