1

I have an application with 2 Windows. I removed the StartupUri in the App.xaml. Instead I subscribed for the Startup-Event to show my Windows.

My two windows are called Login and MainWindow.

In the Startup-Event I have the following code:

private void App_OnStartup(object sender, StartupEventArgs e)
{
    var login = new Login();
    var dialogResult = login.ShowDialog();
    if (dialogResult.HasValue && dialogResult.Value)
    {
        var mainWindow = new MainWindow();
        mainWindow.ShowDialog();
    }
}

When I start my application the Login-Window appears. On the Login-Window there's just one Button. On a click on this Button the following code will be executed:

private void ButtonClick(object sender, RoutedEventArgs e)
{
   DialogResult = true;
   Close();
}

My expectations are now, that on a click on the Button in the Login-Window the MainWindow appears. Instead the complete application is terminated.

If I set a BreakPoint in the StartUp-Event at if (dialogResult.HasValue && dialogResult.Value) I can enter the brackets and the code in the brackets is executing, but the MainWindow does not appear.

Any ideas why this happens?

I can solve this problem by calling the MainWindow in the Click-Event of the Login-Window like.

var mainWindow = new MainWindow();
mainWindow.Show();
Close();
Tomtom
  • 9,087
  • 7
  • 52
  • 95

2 Answers2

1

It´s because the ShutDown-Property of your application is usually set to "OnLastWindowClose".

So, you have to instantiate your MainWindow before your Login-Window is closed or change the Shutdown-Property.

Kai S.
  • 106
  • 2
1

This post might be of interest. It points to this reference which I think describes a similar issue to what you are describing.

Basically the first window that is opened is considered the main window of the application.

I would suggest that you should leave your MainWindow as the default and open your login window in the Loaded event of the MainWindow - if the login is unsuccessful then close the main window (before it gets a chance to open).

Community
  • 1
  • 1
Ben
  • 3,241
  • 4
  • 35
  • 49