Why can't I call โ€œnew window (). ShowDialog ()โ€ 2 times when starting the application?

Here is the XAML code:

<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" /> 

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

The window shows only once, and then the application terminates. Why??

UPDATE: I know that windows should appear accordingly. But after I close the first window, the second does not appear at all

+4
source share
5 answers

try it

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

Insert form comment:

I think that when you close the first window, the application checks for other windows and does not find it (so the application closes), because the second window was not created

+5
source

ShowDialog will not allow you to create the same form if closing it.

This is the difference between a modal and a non-modal form.

I think WPF is for the same reason ...

and you can see โ†“

Display modal and model forms of Windows

UPDATE:

Take the Stecya test and it works great ...

 protected override void OnStartup(StartupEventArgs e) { var w1 = new Window(); var w2 = new Window(); w1.ShowDialog(); w2.ShowDialog(); } 
0
source

Is it possible to say that this will show two windows in sequence, and not simultaneously? When window1 is closed, window2 will automatically open, since the call is ShowDialog (), which opens the window and then sets focus on it and does not open another one until window1 is closed?

0
source

You can use a for loop to do this. Hauverver, I have no idea why you canโ€™t call directly.

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

You may end the entire application in the code used to close window 1. If you use something like Environment.Exit(0); This may be a problem.

0
source

All Articles