English 中文(简体)
为什么关闭最后一个孩子的窗口会将其父母的窗口减到最小?
原标题:Why does closing the last child window minimize its parent window?
  • 时间:2012-05-25 15:53:31
  •  标签:
  • c#
  • wpf

我有以下简单的 wpf 应用程序 :

App.satml : 添加数 :

<Application x:Class="TestWpf2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>

App.squml.cs:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        var parentWindow = new Window();
        parentWindow.Show();

        var childWindow1 = new Window { Owner = parentWindow };
        childWindow1.Show();

        var childWindow2 = new Window { Owner = parentWindow };
        childWindow2.Show();
    }
}

应用程序导致 3 个窗口出现在屏幕上。 如果您运行应用程序并关闭两个子窗口, 父窗口将最小化为任务栏。 如果您对 childWindow2.show () 进行评论, 运行应用程序并关闭单个子窗口, 则父窗口不会最小化为任务栏 。

在围绕这一问题开展工作时,我可以增加以下守则:

childWindow1.Closing += delegate(object sender, CancelEventArgs ex)
{
    (sender as Window).Owner = null;
};

但我不想用这样的黑客, 我想知道为什么这个问题会发生。

为什么会发生这种事?

问题回答

这是 WPF 的无证特征( bug)

这个窃听器七年多前就报告给微软了

非模式窗口顶端的 Modal 对话框将主窗口传回。

The WPF team has recently reviewed this issue and will not be addressing this issue as at this time the team is focusing on the bugs impacting the highest number of WPF developers.

让我们看看这个无证特征的行为。

<强 > 它不会最小化, 只要在后面走一个窗口( 以调试模式, 在视觉工作室窗口后面)

采取步骤证明:

  • 运行此应用程序 。

  • 最小化所有其他窗口( 例如: 视觉工作室和所有其他窗口) 。 只保留屏幕上的这三个窗口 。

  • 父(母)父(母)亲(亲)亲(亲)亲(母)亲(母)亲(母)在 Nomal

Test this code for further proof: StateChange wouldn t fire.

        protected override void OnStartup(StartupEventArgs e)
        {
            var parentWindow = new Window() { Title = "Parent" };
            parentWindow.StateChanged += (sender, ep)=>
            {
                var state = ((Window)sender).WindowState;
            };
            parentWindow.Show();

            var childWindow1 = new Window { Owner = parentWindow, Title = "Child1" };
            childWindow1.Show();

            var childWindow2 = new Window { Owner = parentWindow, Title = "Child2" };
            childWindow2.Show();
        }

< 强势 > 成为顶峰防止发生

如果父窗口是顶峰, 就不会发生这种情况。 但在许多情况下, 这可能不是一个选项 。

parentWindow.Topmost = true;

<强 > 工作选择

在孩子2 关闭时激活父子关系 。

childWindow2.Closed += (a, b) => { parentWindow.Activate(); };

这是因为当孩子的窗口与所有者一起显示时,您无法使用父窗口。只要在孩子在屏幕上的时候尝试访问父窗口,它不会让你看到我的意思。

如果您不指定所有者, 这种行为就不会发生 。

最终,我写下这个代码, 被称作在任何儿童窗口关闭后:

// App inherits from Application, and has a Window property called MainWindow
// and a List<Window> property called OpenWindows.
if (!App.OpenWindows.Any())
    App.ParentWindow.Activate();

如果最后一个子窗口被关闭( 制作 < code> App. OpenWindows. anyi () 假的), 父窗口将被激活。 这不会导致闪烁( 例如, 主窗口会最小化, 然后最大化 ) 。

这不是最好的解决方案。 我将不结束这个问题, 希望有人能解释为什么WPF有这个功能!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签