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

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

问题描述

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



我解决了问题,但我希望有人能够启发我为什么会发生这种情况。



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



它很容易复制,所以这里是一些非常自负的代码来说明问题。这个代码是完整的,所以使用这个,你应该能够重现它。



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

 < Window x:Class =ForumExampleShowDialogIssue.OKDialogWindow
xmlns =http://
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title =OKDialogWindowHeight = 300Width =300>
< StackPanel>
< TextBlock Text =这是一个带有单个按钮的窗口,该按钮设置为取消,因此关闭窗口。
TextWrapping =Wrap
Margin =5/>
< Button Content =OKIsCancel =TrueIsDefault =True
Margin =5/>
< / StackPanel>



接下来,使用标准WPF 应用程序类,XAML中没有新内容,但代码中包含以下代码:

  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标题中引用它。



运行。 (程序,而不是森林)



这样可以预期。 alwaysOpen 窗口保持打开,而一个接一个的 dialogWindow 实例出现在对话框模式下,当 OK 被点击时关闭,然后显示下一个。



然而,当您将 OnStartup 更改为以下内容时,会中断: strong>

  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);
}

}

当没有不断打开的窗口,第一个对话窗口是唯一的对话窗口。之后,无数显示前和后显示消息被打印到控制台,但没有出现新的对话框窗口 - 一旦显示,它们就会自动关闭。



这不可能是预期的行为吗?你会得到相同的结果吗?任何想法为什么会发生这种情况?

解决方案

这是预期的行为。




  • 默认情况下,第一个打开的窗口是MainWindow。

  • 默认情况下,列表成为MainWindow(如果其他的要删除)。

  • 如果
    窗口列表中没有窗口,应用程序类的设计将退出。



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


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.

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>

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");
    }
}

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

Run. (the program, not like Forest).

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.

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.

Surely this can't be the intended behaviour? Do you get the same result? Any idea why this happens?

解决方案

This is the intended behavior.

  • by Default the first open window is the MainWindow.
  • By default the only window in the list becomes the MainWindow (if others are to be removed).
  • Application Class is designed to exit if no windows are present in windows list.

Check this: http://www.ageektrapped.com/blog/the-wpf-application-class-overview-and-gotcha/

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

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