In your MainWindow.xaml.cs
, try doing this:
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Application.Current.Shutdown();
}
Per this link, you can also set the ShutdownMode
in XAML:
http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
ShutdownMode="OnExplicitShutdown"
>
</Application>
Applications stop running only when the Shutdown
method of the Application
is called. Shut down can occur implicitly or explicitly, as specified by the value of the ShutdownMode
property.
If you set ShutdownMode
to OnLastWindowClose
, Windows Presentation Foundation (WPF) implicitly calls Shutdown when the last window in an application closes, even if any currently instantiated windows are set as the main window (see MainWindow).
A ShutdownMode
of OnMainWindowClose
causes WPF to implicitly call Shutdown when the MainWindow closes, even if other windows are currently open.
The lifetime of some applications may not be dependent on when the main window or last window is closed, or may not be dependent on windows at all. For these scenarios you need to set the ShutdownMode
property to OnExplicitShutdown
, which requires an explicit Shutdown
method call to stop the application. Otherwise, the application continues running in the background.
ShutdownMode
can be configured declaratively from XAML or programmatically from code.
This property is available only from the thread that created the Application
object.
In your case, the app isn't closing because you're probably using the default OnLastWindowClose
:
If you set ShutdownMode
to OnLastWindowClose
, WPF implicitly calls Shutdown when the last window in an application closes, even if any currently instantiated windows are set as the main window (see MainWindow
).
Since you're opening a new window, and not closing it, shutdown doesn't get called.