WPF App loses completely focus on window close

前端 未结 8 2214
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 20:40

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 Messa

相关标签:
8条回答
  • 2020-12-12 21:12

    Maybe i am quite late on the matter.
    @HCL this problem could be resolved more easily by setting the options parameter of the MessageBox.show() to MessageBoxOptions.None.

    Cheers...

    0 讨论(0)
  • 2020-12-12 21:18

    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.

    0 讨论(0)
  • i had a similar problem. I had a one main window, where rest child windows was assign to that window via Owner property. If main window had two or more children, after close second child, app lose focus. I used trick with child.Owner = null before close, but then my next child window lose focus. I handled it by getting all children and set focus to the last one before closing child. Here is code example:

    private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
            {
                var childWindows = (sender as Window).Owner.OwnedWindows;
                if (childWindows.Count - 2 >= 0)
                    childWindows[childWindows.Count - 2].Focus();
                else
                {
                    (sender as Window).Owner.Focus();
                }
                (sender as Window).Owner = null;
            }
    
    0 讨论(0)
  • 2020-12-12 21:21

    it is possible to write your own messagebox, you can implement it as a floater the disable entire screen when showed.

    you check the caliburn mvvm frameork sample, there is a nice implementation of messagebox as service.

    0 讨论(0)
  • 2020-12-12 21:23

    This is a very annoying bug, the most common solution provided is:

    call Me.Owner.Focus in the Window_Unloaded or Window_Closing event.

    But that still doesnt work 100%, you still see the background-window flashing to the foreground (very briefly), before focus is restored correctly.

    I found a method which works better:

    call Me.Owner.Activate before Me.Close (or in the _Closing event).

    0 讨论(0)
  • 2020-12-12 21:26

    2020 and still in trouble...

    Workaround for Wpf :

        public ContactsWindow()
        {
            InitializeComponent();
    
            Closing += ContactsWindow_Closing;
        }
    
        private void ContactsWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Owner?.Activate();
        }
    

    It seems to work for me, interested in any news

    0 讨论(0)
提交回复
热议问题