WPF应用程序有失完全集中在窗口关闭 [英] WPF App loses completely focus on window close

查看:150
本文介绍了WPF应用程序有失完全集中在窗口关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题说明

如果我做一个非模态窗口的子窗口,通过该窗口的所有者设置为一个父窗口,然后显示从这个子窗口中的一个消息,父窗口就会失去焦点,如果我关闭子窗口。如果Windows资源管理器或其他应用程序打开时,这个程序将获得焦点,我的主窗口会被隐藏起来。

If I make a non-modal window as a child window through setting the Owner of the window to a parent window, and then show a MessageBox from within this child window, the parent window will lose focus if I close the child window. If windows explorer or another app is open, this app will get the focus and my main window will be hidden.

这似乎是一个已知的问题,因为我看到了它的另一<一href="http://social.msdn.microsoft.com/forums/en-US/wpf/thread/a51ac7b9-7021-4669-accd-af0e86b9cc01">newsgroups,但我没有看到一个很好的解决方案。所有者设置在ondelete在空不是一个选项。设置所有者显示在MessageBox之前为空,之后重新于事无补。设置所有者空的OnClosed事件不会也没有帮助。

This seems to be a known problem as I saw it in another newsgroups, but I don’t have seen a good solution. Setting the owner to null in OnDeactivate is not an option. Setting the owner before showing the MessageBox to null and resetting after that doesn’t help. Setting the owner to null in the OnClosed event does also not help.

简单的解决方案中找到

如果您遇到同样的问题,我刚才所说的,把所有的子窗口的下面code在OnClosing。

If you experience the same problem as I have described, put the following code in the OnClosing of all child windows.

void  OnClosing(System.ComponentModel.CancelEventArgs e)
  base.OnClosing(e);
  if (null != Owner) {
         Owner.Activate();
  }
  // ....

有可以跟随任何进一步的处理逻辑,甚至开口提示消息是可以容忍的。

It can be followed by any further processing logic, even opening MessageBoxes is tolerated.

例 - code

这个问题似乎是,因为我以为要大得多。下面的例子将删除父窗口的焦点,如果消息框将被打开,子窗口将被关闭(复制code到加载事件处理程序中的窗口)。

The issue seems to be much bigger as I thought. The following example will remove focus of the parent window if the message box will be opened and the the child window will be closed (Copy the code into a loaded event-handler of a Window).

Window firstChildWindow = new Window() {
    Title = "Floating Window", Width = 100, Height = 70
};
firstChildWindow.Owner = Window.GetWindow(this);

Button button = new Button() { Content="MessageBox"};
button.Click += delegate { 
    MessageBox.Show("Klicking her breaks the focus-chain."); };
firstChildWindow.Content = button;
firstChildWindow.Show();

另外这个例子破坏的焦点链:

Also this example breaks the focus-chain:

Window firstChildWindow = new Window() {
    Title = "Floating Window", Width = 100, Height = 70
};
firstChildWindow.Owner = Window.GetWindow(this);
firstChildWindow.Show();

Window secondChildWindow = new Window() { 
    Title="Second Window",Width=100,Height=70};
secondChildWindow.Content = new TextBlock() { 
    Text="SecondWindow"};
secondChildWindow.Owner = firstChildWindow;
secondChildWindow.Show();

有一个人对这个问题的决议。我想一劈触发给予重点父成交后,与调度员或DispachterTimer或许这将是工作manualy力重点在封闭的父,但这一切似乎对我很不洁(并且也有点复杂,如果有相同父的更积极拥有的窗口,因为我在我的当前应用程序)。

Has someone a resolution for this problem. I think about a hack to trigger giving focus to the parent after closing, with Dispatcher or DispachterTimer or perhaps it would be work to manualy force focus to the parent on closed but this all seems to me very unclean (and is also a little complicated if there are more active owned windows of the same parent, as I have in my current app).

没有人知道一个整洁的解决办法?

No one knows a neat solution to this?

资源

MSDN说明(下言论已开过非模态窗口看到调用展())

MSDN Description (see under remarks for non modal windows opened calling Show())

<一个href="http://social.msdn.microsoft.com/forums/en-US/wpf/thread/a51ac7b9-7021-4669-accd-af0e86b9cc01">Same在MSDN论坛问题没有适当的解决方案

请参见:WPF的失稳重点应用

推荐答案

最有可能发生在这里的是你有两个独立的顶级窗口和关闭其中之一。这有时会导致焦点跳到另一个应用程序。

What most likely happened here is you have two independent top level windows and closed one of them. This will sometimes cause focus to jump to another application.

这也恰好如果一个窗口拥有第二个,这反过来又拥有三分之一。在Win32 API中可能的错误,但它一直伴随着我们永远这么好运气得到它固定。

This also happens if one windows owns a second, which in turn owns a third. Probable BUG in the Win32 API but its been with us forever so good luck getting it fixed.

的解决方法是手动手焦点回到孩子在孙子的Closing事件。但是,在这种情况下,孙子是一个消息,所以你不能这样做。最容易做的事情就是把自己的MessageBox父。下一个最简单的就是让自己的消息框控件。

The workaround is to manually hand focus back to child in the Closing event of the grandchild. But in this case, grandchild is a messagebox so you can't do that. The easiest thing to do is to own messagebox to parent. The next easiest is to make your own messagebox control.

这篇关于WPF应用程序有失完全集中在窗口关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆