Search code examples
c#wpfwindow

C# WPF How to close a new window correctly?


I have a simple question:

1- Create a new WPF project, there's startup window in it: MainWindow.xaml.

2- In this project, create a new window Window1.xaml.

3- Windows1's loaded event, let it close.

4- Put an Open button to MainWindow. Implement it's click event.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    this.Close();
}

private void button_Click(object sender, RoutedEventArgs e)
{
    Window1 w = new Window1();
    w.Show();
}

When I start this application, VS2015's UI became to the DEBUGING MODE, then I click the close button on the right top corner of the Window, VS2015's UI back to normal mode.

Now, if I start application, click Open button, then Window1 will show quickly and closed, but, if I click the close button on the right top corner of MainWindow, things different: VS2015 will no go back to normal mode but stay in DEBUGING MODE. so to me, that means something hang there and I don't know what is it.

Is there anyone know how to fix this problem?


Solution

  • In the App.xaml set:

    ShutdownMode="OnMainWindowClose"
    

    This must solve the problem. I recommend reading this question.