Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

.net - WPF App loses focus completely on window close

Problem description

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.

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.

Simple Solution found

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.

Example-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();

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 manually 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?

Resources

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

Same problem on MSDN forums without appropriate solution

Please see also: Instable focus of WPF apps

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...