Reputation: 8474
Here's 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" />
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. Why??
UPDATE: I know that windows should show up consequently. But after I close first window second does not show up at all
Upvotes: 5
Views: 2968
Reputation: 23266
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
Upvotes: 6
Reputation: 4294
ShowDialog wont allow you to create a same form unless its closed.
It's the difference between a modal and modeless form.
I think WPF is as the same reason...
and you can see ↓
Display Modal and Modeless Windows Forms
UPDATE:
Take a test by Stecya's answer , and it work fine...
protected override void OnStartup(StartupEventArgs e)
{
var w1 = new Window();
var w2 = new Window();
w1.ShowDialog();
w2.ShowDialog();
}
Upvotes: 0
Reputation: 2655
You are possibly ending the entire application in the Code used to close Window 1. If you are using something like Environment.Exit(0);
this could be the issue.
Upvotes: 0
Reputation: 686
You can use a for loop to do so. Howerver, I have no idea why can't call directly.
for (int i = 0; i < 2; i++)
{
new Window().ShowDialog();
}
Upvotes: 0
Reputation: 135
Am I right to say that this will show the two windows consecutively and not simultaneously? 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?
Upvotes: 0