Wpf “调用线程必须是STA”错误,尽管该线程是STA

Wpf “调用线程必须是STA”错误,尽管该线程是STA,wpf,exception,invoke,dispatcher,Wpf,Exception,Invoke,Dispatcher,我在WPF中的STA线程有点问题 我做了以下工作: Dispatcher.BeginInvoke(new Action(CreateAndShow), null); CreateAndShow的外观如下所示: _mainWindow = new PresentWindow(); // not really important what happens in the next lines, the error is before this AddPerspectives(); MainWind

我在WPF中的STA线程有点问题

我做了以下工作:

Dispatcher.BeginInvoke(new Action(CreateAndShow), null);
CreateAndShow的外观如下所示:

_mainWindow = new PresentWindow();
// not really important what happens in the next lines, the error is before this
AddPerspectives(); 
MainWindow = _mainWindow;
MainWindow.Show();
现在,在PresentWindow构造函数开始的那一行,我得到了一个异常,说明调用线程需要是STA线程

我在创建PresentWindow之前输入了一个断点。当前线程的状态为STA。如果我在没有调用的情况下从不同的点调用CreateAndShow,但是直接在完全相同的STA线程中调用,那么它工作得非常好

我相信这一切都有一个非常简单的解释,但我找不到。有人能帮忙吗


谢谢

您正在构造函数中创建线程吗?不,构造函数只是调用由VS创建的InitializeComponent,并创建一些列表和事件处理程序。但是在构造函数中的某个地方,但在它的一开始,不会引发异常。它位于linepublic PresentWindow中-顺便问一下,如何在此注释文本框中换行?请尝试shift+enter。是否在新线程中创建窗口?如果您所做的只是调用InitializeComponent,那么new PresenWindow抛出异常是没有意义的。要粘贴完整或更多代码吗?