Window.Show() 显示新窗口,然后消失在主窗口后面

发布于 2024-12-07 08:51:35 字数 490 浏览 1 评论 0原文

我正在开发一个 Prism 应用程序,我需要在新窗口中发布视图模型。为了实现这一目标,我创建了一个专门用于发布这些视图模型的服务。我这样称呼它:

windowService.Publish(myViewModel);

Publish 方法的主体如下:

public void Publish(FloatingViewModel viewModel)
{
    var floatingWindow = ServiceLocator.Current.GetInstance<FloatingWindow>();

    floatingWindow.DataContext = viewModel;
    floatingWindow.Show();
}

Show() 方法执行后,新窗口快速显示在我的主窗口上方,然后消失在我的主窗口后面,就像它收回了焦点一样。

有谁知道这种行为可能来自哪里?

I am developing a Prism application where I need to publish view-models in a new window. To achieve that I created a service dedicated to the publication of these view-models. I call it like this:

windowService.Publish(myViewModel);

And the body of the Publish method is like this:

public void Publish(FloatingViewModel viewModel)
{
    var floatingWindow = ServiceLocator.Current.GetInstance<FloatingWindow>();

    floatingWindow.DataContext = viewModel;
    floatingWindow.Show();
}

After the Show() method execution, the new window quickly shows up above my main window and then disappears behind my main window like if it took back the focus.

Does anybody knows where this behavior could come from?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

百善笑为先 2024-12-14 08:51:35

我终于找到了问题所在,而且问题实际上比我想象的要深得多。 此答案与所描述的问题无关,因为根本原因完全不同。

在窗口中发布视图模型的代码实际上嵌套在 WPF 命令中,并且该命令通过 InvokeCommandAction 附加到 MouseDoubleClick 事件。我发现,即使将事件绑定到命令,该事件也不会被标记为已处理,因此会传播直到它被处理。这导致我的主窗口收回焦点。

I finally found the problem and it was actually way deeper than I thought. This answer will not be relevant of the described problem as the root cause was totally different.

The code that was publishing the view-model in a window was actually nested in a WPF command and this command was attached to the MouseDoubleClick event through a InvokeCommandAction. I discovered that even when you bind an event to a command, the event is not marked as handled and therefore will propagate until it is handled. That was causing my main window to take back the focus.

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