获取“调用线程必须是 STA”使用 MVVM Light 消息创建新对话框窗口时
当我的视图视图模型中收到消息时,会显示一个对话框窗口 对话框
如下 http://pastebin .com/BAeCLwhz (我知道我应该有一个空的代码隐藏,但使其为空是第二优先级,所以现在这不是问题!)
我在我的项目视图模型中创建了窗口
MessengerInstance.Register<bool>(this, "Homing", ShowHomingDialog);
private void ShowHomingDialog(bool b)
{
HomingRobot hb = new HomingRobot();
hb.ShowDialog();
}
,但是当它运行时我得到“调用线程必须是 STA”我尝试在项目代码隐藏中而不是在视图模型中调用对话框,但这给了我相同的结果。那么我该如何让我的窗口弹出而不是“多线程”,这在使用消息时甚至是可能的呢?
I have a dialog window that is shown when a message is recived in my Views View-Model
The Dialog is as here http://pastebin.com/BAeCLwhz (I know I should have a empty codebehind, but making it empty is second priority, so that is not an issue now!)
I create the window in my Projects View-Model with
MessengerInstance.Register<bool>(this, "Homing", ShowHomingDialog);
private void ShowHomingDialog(bool b)
{
HomingRobot hb = new HomingRobot();
hb.ShowDialog();
}
But when its run I get "Calling thread must be STA" I tried calling the dialog on the projects codebehind instead of in the viewmodel, but that gave me the same result. So how am I supposed to make my window pop up without it being "multithreaded" is that even possible when using messages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 ShowDialog 代码包装在 Dispatcher.Invoke 中,如下所示
如果上面的代码不起作用,您可以尝试以下代码
You can wrap the ShowDialog code in the Dispatcher.Invoke as shown below
If the above code doesn't work, you can try the following
您需要将您的 UI 工作“分派”到 UI 线程。
请参阅此提示 http://www. silverlightshow.net/items/Tip-Asynchronous-Silverlight-Execute-on-the-UI-thread.aspx
You need to "dispatch" your UI work to the UI thread.
See this tip http://www.silverlightshow.net/items/Tip-Asynchronous-Silverlight-Execute-on-the-UI-thread.aspx