获取“调用线程必须是 STA”使用 MVVM Light 消息创建新对话框窗口时

发布于 2024-11-07 14:53:11 字数 529 浏览 0 评论 0原文

当我的视图视图模型中收到消息时,会显示一个对话框窗口 对话框

如下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2024-11-14 14:53:11

您可以将 ShowDialog 代码包装在 Dispatcher.Invoke 中,如下所示

Dispatcher.CurrentDispatcher.Invoke((Action)(() =>
{
    HomingRobot hb = new HomingRobot();        
    hb.ShowDialog();
}));

如果上面的代码不起作用,您可以尝试以下代码

Application.Current.Dispatcher.Invoke((Action)(() =>
{
    HomingRobot hb = new HomingRobot();        
    hb.ShowDialog();
}));

You can wrap the ShowDialog code in the Dispatcher.Invoke as shown below

Dispatcher.CurrentDispatcher.Invoke((Action)(() =>
{
    HomingRobot hb = new HomingRobot();        
    hb.ShowDialog();
}));

If the above code doesn't work, you can try the following

Application.Current.Dispatcher.Invoke((Action)(() =>
{
    HomingRobot hb = new HomingRobot();        
    hb.ShowDialog();
}));
娇纵 2024-11-14 14:53:11

您需要将您的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文