简体   繁体   English

为什么我不能在应用程序启动中调用 'new Window().ShowDialog()' 2 次?

[英]Why I can't call 'new Window().ShowDialog()' 2 times in Application startup?

Here's the XAML code:这是 XAML 代码:

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

Backing code:支持代码:

using System.Windows;

namespace WpfApplication2
{
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            new Window().ShowDialog();
            new Window().ShowDialog();
        }
    }
}

Window shows only one time and then application exits. Window 只显示一次,然后应用程序退出。 Why??为什么??

UPDATE: I know that windows should show up consequently.更新:我知道 windows 应该因此出现。 But after I close first window second does not show up at all但是在我关闭第一个 window 之后,第二个根本没有出现

Try this尝试这个

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        var w1 = new Window();
        var w2 = new Window();

        w1.ShowDialog();
        w2.ShowDialog();
    }

Paste form comment:粘贴表单注释:

I think when you close first window,application checks whether there are other windows,and it doesn't find any (so application is closing), because second window haven't been created我想当你关闭第一个 window 时,应用程序检查是否还有其他 windows,它没有找到任何(所以应用程序正在关闭),因为第二个 window 尚未创建

ShowDialog wont allow you to create a same form unless its closed. ShowDialog 不允许您创建相同的表单,除非它关闭。

It's the difference between a modal and modeless form.这是模态形式和非模态形式之间的区别。

I think WPF is as the same reason...我认为 WPF 是同样的原因......

and you can see ↓你可以看到↓

Display Modal and Modeless Windows Forms显示模态和非模态 Windows Forms

UPDATE:更新:

Take a test by Stecya 's answer, and it work fine...通过Stecya的答案进行测试,它工作正常......

protected override void OnStartup(StartupEventArgs e)
    {
        var w1 = new Window();
        var w2 = new Window();

        w1.ShowDialog();
        w2.ShowDialog();
    }

Am I right to say that this will show the two windows consecutively and not simultaneously?我可以说这将连续而不是同时显示两个 windows 吗? When window1 is closed window2 will automatically open as the call is ShowDialog() which opens the window and then sets focus to it and doesn't open the other one until window1 is closed?当 window1 关闭时,window2 将自动打开,因为调用是 ShowDialog(),它打开 window 然后将焦点设置为它并且在 window1 关闭之前不会打开另一个?

You can use a for loop to do so.您可以使用 for 循环来执行此操作。 Howerver, I have no idea why can't call directly.但是,我不知道为什么不能直接打电话。

       for (int i = 0; i < 2; i++)
       {
            new Window().ShowDialog();
       }

You are possibly ending the entire application in the Code used to close Window 1. If you are using something like Environment.Exit(0);您可能在用于关闭 Window 1 的代码中结束整个应用程序。如果您使用的是Environment.Exit(0); this could be the issue.这可能是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM