没有其他窗口打开时 System.Windows.Window.ShowDialog() 的意外行为.知道为什么吗? [英] Unexpected behaviour of System.Windows.Window.ShowDialog() when no other windows are open. Any idea why?

查看:18
本文介绍了没有其他窗口打开时 System.Windows.Window.ShowDialog() 的意外行为.知道为什么吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的 WPF MVVM 应用程序尝试在主窗口启动之前显示两个连续的错误对话框窗口时,我发现了这一点(付出了一些努力):确定第一个窗口后,应用程序进入循环,第二个错误对话框从来没有出现过.

I picked up on this (with some effort) when my WPF MVVM application tried to show two consecutive error dialog windows before the main window was launched: After OKing the first window, the application went into a loop and the second error dialog never showed up.

我解决了这个问题,但我希望有人能告诉我为什么会这样.

I fixed the problem, but I was hoping someone could enlighten me as to why this happened.

看起来,如果没有非模态打开的窗口,如果一个对话框窗口已经关闭,所有新的对话框窗口都立即关闭,不显示.

It seems that, if there are no non-modal open windows, if one dialog window has been closed, all new dialog windows are immediately closed, without displaying.

它很容易重现,所以这里有一些非常自负的代码来说明这个问题.这段代码是完整的,所以只用这个,你应该可以重现它.

Its very easy to reproduce, so here is some highly conceited code to illustrate the problem. This code is complete, so using just this, you should be able to reproduce it.

为对话框窗口创建一个 Window 控件,没有代码,只有以下 XAML:

Create a Window control for the dialog window, with no code behind, and just the following XAML:

<Window x:Class="ForumExampleShowDialogIssue.OKDialogWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="OKDialogWindow" Height="300" Width="300">
<StackPanel>
    <TextBlock Text="This is a Window with a single button. The button is set to Cancel, so it closes the window."
               TextWrapping="Wrap"
               Margin="5"/>
    <Button Content="OK" IsCancel="True" IsDefault="True"
            Margin="5"/>
</StackPanel>

接下来,使用标准的 WPF App 类,在 XAML 中没有新内容,但在后面的代码中有以下内容:

Next, use the standard WPF App class, with nothing new in the XAML, but with the following in the code behind:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    OKDialogWindow alwaysOpen = new OKDialogWindow();
    alwaysOpen.Show();

    while (true)
    {                
        OKDialogWindow dialogWindow = new OKDialogWindow();
        Console.WriteLine("Before show");
        dialogWindow.ShowDialog();
        Console.WriteLine("After show");
    }
}

删除 MainWindow.XAML(如果存在),并从 App.XAML 标头中删除对它的引用.

Delete the MainWindow.XAML if it exists, and remove the reference to it from the App.XAML header.

运行.(程序,不像 Forest).

Run. (the program, not like Forest).

这按预期工作. alwaysOpen 窗口保持打开状态,而一个接一个的 dialogWindow 实例以对话框模式出现,当 OK 被点击,然后显示下一个.

This works as expected. The alwaysOpen window remains open, while one after the other dialogWindow instances appear in dialog mode, closing when OK is clicked, then showing the next one.

但是,当您将 OnStartup 更改为以下内容时,这会中断:

HOWEVER, this breaks when you change OnStartup to the following:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    while (true)
    {                
        OKDialogWindow dialogWindow = new OKDialogWindow();
        Console.WriteLine("Before show");
        dialogWindow.ShowDialog();
        Console.WriteLine("After show");
    }

}

当没有持续打开的窗口时,第一个对话窗口是唯一有效的.之后,无数的演出前"和演出后"消息被打印到控制台,但没有出现新的对话框窗口 - 它们一显示就自动关闭.

When there is no constantly open window, the first dialog window is the only one that works. After that, countless "Before show" and "After show" messages are printed to the console, but no new dialog windows appear - they are closed automatically as soon as they are shown.

这肯定不是预期的行为吗?你得到同样的结果吗?知道为什么会这样吗?

推荐答案

这是预期的行为.

  • 默认情况下,第一个打开的窗口是 MainWindow.
  • 默认情况下,列表中的唯一窗口成为 MainWindow(如果要删除其他窗口).
  • 应用程序类被设计为在没有窗口存在时退出窗口列表.

检查这个:http://www.ageektrapped.com/blog/the-wpf-application-class-overview-and-gotcha/

这篇关于没有其他窗口打开时 System.Windows.Window.ShowDialog() 的意外行为.知道为什么吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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