主窗口落后于其他应用程序的窗口就会消失一个子窗口中的第三个窗口上使用后的ShowDialog [英] Main window disappears behind other application's windows after a sub window uses ShowDialog on a third window

查看:178
本文介绍了主窗口落后于其他应用程序的窗口就会消失一个子窗口中的第三个窗口上使用后的ShowDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在WPF应用程序这个非常奇怪的行为。



我有一个主窗口,这是表明,使用显示() App.OnStartup 。说主窗口可以打开一个(非模态)子窗口,也使用显示()。的子窗口 所有者设置为主窗口



子窗口关闭,主窗口将再次可见(好)。



某些操作可能会导致子窗口打开第三个窗口为模态对话框,使用的ShowDialog()所有者设置为子窗口)。当该模式对话框的子窗口,然后奇怪的事情发生在生命周期内开启和关闭至少一次。



结束子窗口之后,主窗口不映入眼帘。相反,无论随机窗口的背后主窗口映入眼帘。任何人都可以向我解释为什么出现这种情况,如何解决?



这没有什么区别模态对话框是否是一个正常的窗口使用显示的ShowDialog(),或使用显示一个消息框 MessageBox.Show()






下面是一些的最小的代码来重现此。在Visual Studio中的一个新的WPF应用程序,并粘贴到预生成MainWindow.xaml.cs



然后,按键盘上的按键只开一个窗口,关闭它,行为符合市场预期。按下两个键,关闭两个,那么第一个窗口是Visual Studio中(大概)的后面。

 公共主窗口()
{
的InitializeComponent();
this.PreviewKeyDown + =(发件人,E)=>
{
如果(this.Owner是主窗口)
{
//我们的子窗口

MessageBox.Show(我是一个模式对话);低于

//代码产生完全相同的行为,消息框

// VAR对话框=新的主窗口();
//dialog.Owner =这一点;
//dialog.ShowDialog();
}
,否则
{
//我们通过应用程序创建的初始主窗口。
无功子窗口=新的主窗口();
subWindow.Owner =这一点;
subWindow.Show();
}
};
}


解决方案

这是一个非常恼人的WPF错误,我从来没有发现,导致它的代码缺陷,但有得摸不着头脑的源代码,与重点涉及意见的heckofalot。只是一种变通方法,一个不到理想的,可以明确地给予重点主人,当窗口正在关闭解决这个问题。复制/粘贴此代码在你的子窗口类;

 保护覆盖无效OnClosing(System.ComponentModel.CancelEventArgs E){
base.OnClosing(E);
如果(e.Cancel&安培;!&安培;!this.Owner = NULL)this.Owner.Focus();
}


I have noticed this very odd behavior in a WPF application.

I have a MainWindow, which is shown using Show() from App.OnStartup. Said MainWindow can open a (non-modal) SubWindow, also using Show(). SubWindow's Owner is set to MainWindow.

When SubWindow is closed, MainWindow is visible again (good).

Some actions can cause the SubWindow to open a third window as a modal dialog, using ShowDialog() (Owner is set to SubWindow). When that modal dialog is opened and closed at least once during the lifetime of a SubWindow, then the weird thing happens.

After closing SubWindow, MainWindow does not come into view. Instead, whatever random window is behind MainWindow comes into view. Can anyone explain to me why this happens, and how to fix it?

It makes no difference whether the modal dialog is a normal Window displayed using ShowDialog(), or a message box shown using MessageBox.Show().


Here is some minimal code to reproduce this. Create a new WPF application in visual studio, and paste this into the pre-generated MainWindow.xaml.cs

Then, press a key on the keyboard to open only one window, close it, behavior as expected. Press two keys, close both, then the very first window is behind Visual Studio (presumably).

public MainWindow()
{
    InitializeComponent();
    this.PreviewKeyDown += (sender, e) =>
    {
        if (this.Owner is MainWindow)
        {
            // we're the SubWindow

            MessageBox.Show("I am a modal dialog");

            // code below produces the exact same behavior as the message box

            //var dialog = new MainWindow();
            //dialog.Owner = this;
            //dialog.ShowDialog();
        }
        else
        {
            // we're the initial MainWindow created by App.
            var subWindow = new MainWindow();
            subWindow.Owner = this;
            subWindow.Show();
        }
    };
}

解决方案

This is a pretty annoying WPF bug, I never did find the flaw in the code that causes it but there's a heckofalot of "gotta figure this out" comments in the source code that deals with focusing. Just a workaround, a less than ideal one, you can solve it by explicitly giving the focus to the owner when the window is closing. Copy/paste this code in your SubWindow class;

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
        base.OnClosing(e);
        if (!e.Cancel && this.Owner != null) this.Owner.Focus();
    }

这篇关于主窗口落后于其他应用程序的窗口就会消失一个子窗口中的第三个窗口上使用后的ShowDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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