WPF application does not close when closing the main window

I use WinForms for programming in Visual Studio, but I want to try WPF.

I added another window to my project called Window01. The main window is called MainWindow. Before the public MainWindow() constructor, I declare Window01:

 Window01 w1; 

Now I create this window in:

 private void Window_Loaded(object sender, RoutedEventArgs e) { w1 = new Window01(); } 

I have a button where the window is displayed: w1.ShowDialog(); .

The “funny thing” here is that if I run the application (with debugging) and exit it a few seconds after (I do nothing in the application), Visual Studio does not stop debugging as if the application was still running.

If I move the line w1 = new Window01(); on the button click method, which means just above ShowDialog() , Visual Studio behaves correctly - that is, debugging stops when I exit the application.

Why is this weird behavior?

+50
c # visual-studio-2010 wpf window
Apr 03 2018-12-12T00:
source share
5 answers

In MainWindow.xaml.cs try the following:

 protected override void OnClosed(EventArgs e) { base.OnClosed(e); Application.Current.Shutdown(); } 

From this link you can also install 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 only when the Shutdown Application method is called. Shutdown can be done implicitly or explicitly, as indicated by the value of the ShutdownMode property.

If you set ShutdownMode to OnLastWindowClose , Windows Presentation Foundation (WPF) implicitly invokes Shutdown when the last window in the application closes, even if the current window is set to the main window (see MainWindow).

A ShutdownMode of OnMainWindowClose causes WPF to implicitly call Shutdown when closing MainWindow, even if other windows are currently open.

The lifetime of some applications may not depend on whether the main window or the last window is closed or does not depend on windows at all. For these scenarios, you need to set the ShutdownMode property to OnExplicitShutdown , which requires an explicit call to the Shutdown method to stop the application. Otherwise, the application continues to run in the background.

ShutdownMode can be configured declaratively from XAML or programmatically from code.

This property is only available from the thread that created the Application object.




In your case, the application does not close, because you are probably using OnLastWindowClose by default:

If you set ShutdownMode to OnLastWindowClose , WPF implicitly invokes Shutdown when the last window in the application closes, even if any current Windows instances are set as the main window (see MainWindow ).

Since you open a new window and do not close it, shutting down does not cause.

+89
Apr 03 2018-12-12T00:
source share

I am glad that you received your answer, but for the sake of others I will answer your question to add some information.




Step 1

Firstly, if you want your program to exit when the main window closes, you need to specify, as these are not WinForms, where this is the default behavior.

(The default value in WPF when closing the last window)

In code

Go to the instance of your application at your entry point. (In the VS $ WPF program, by default it is nested inside App.xaml , so go inside and go to App.xaml.cs and create a constructor).

In the constructor, specify that Application ShutdownMode should be ShutdownMode . OnLastWindowClose .

  public App() { ShutdownMode = ShutdownMode.OnLastWindowClose; } 

In xaml

Go to your App.xaml file (or create one yourself) The root is Application , specify inside the Application ShutdownMode should be ShutdownMode . OnLastWindowClose .

 <Application x:Class="WpfApplication27.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" ShutdownMode="OnMainWindowClose"> 

If this works, everything is ready; you can stop reading.




Step 2

If the above did not work (I think you wrote the WPF application from scratch), the main window is probably not known to the application as the main window. Therefore, indicate this as-well.

In code

Go to the application designer, as in step 1, and specify that Application . MainWindow is Window :

 MainWindow = mainWindow; 

In xaml

Go to Application XAML, as in step 1, and specify that Application . MainWindow Window value:

 MainWindow = "mainWindow"; 



Alternative

I don't think this is the best approach, simply because WPF does not want you to do this (therefore it is Application ShutdownMode ), but you can just use the event / override the event method (OnEventHappened).

Go to the file with the code below, and then add:

  protected override void OnClosed(EventArgs e) { base.OnClosed(e); App.Current.Shutdown(); } 
+22
Dec 11 '12 at 12:55
source share

Since the default shutdown mode in the WPF application is OnLastWindowClose, this means that the application stops when the last window is closed.

When you instantiate a new Window object, it is automatically added to the list of windows in the application. So the problem was that your application created two windows at startup - MainWindow and not yet shown Window01 - and if you just closed MainWindow, Window01 will support your application.

Usually you will create a window object in the same method that ShowDialog will call, and each time a dialog box is shown, you will create a new window object.

+10
Apr 03 2018-12-12T00:
source share

It looks like what I came across when I created a second window to act like a dialog box. When the second window was opened and then closed, and then the main window was closed, the application continued to work (in the background). I added (or confirmed) the following in my App.xaml:

 <Application x:Class="XXXXXXX.App" ... StartupUri="MainWindow.xaml" ShutdownMode="OnMainWindowClose"> <Application.MainWindow > <NavigationWindow Source="MainWindow.xaml" Visibility="Visible" /> </Application.MainWindow> 

There is no joy.

So, I finally went into my “MainWindow.xaml” and added the “Closed” property to the Window, which switched to the “MainWind_Closed” method, which looks like this:

  private void MainWind_Closed(object sender, EventArgs e) { foreach ( Window w in App.Current.Windows ) { if (w.DataContext != this) w.Close(); } } 

Running the debugger, it seems that the only window that is displayed is the window that I created as a dialog, in other words, the foreach loop finds only one window - the dialog, and not the main one.

I had "this.Close ()" in the method that closed the dialog, and I had "dlgwin.Close ()" that appeared after "dlgwin.ShowDialog ()", and this did not work, Even "dlgwin = null "

So, why not close this dialogue without this extra material? Well. It works.

0
Apr 7 '16 at 1:18
source share

I stumbled upon this question when I was looking for something else, and I was surprised to not see any of the suggested answers mentioning Window.Owner .

  { var newWindow = new AdditionalWindow(); newWindow.Owner = Window.GetWindow(this); // then later on show the window with Show() or ShowDialog() } 

Calling Window.GetWindow(this) very useful in representing the MVVM application, when you are far from the visual tree, not knowing where you were created, you can call it by providing any FrameWork element (for example, UserContol , Button , Page ). Obviously, if you have a direct link to the window, use this or even Application.Current.MainWindow .

This is a pretty powerful connection that has a number of useful advantages that you might not understand for a start (assuming that you did not specifically encode individual windows to avoid this relationship).

If we call your main window MainWindow , and the second - AdditionalWindow , then ....

  • Minimizing MainWindow also minimizes AdditionalWindow
  • Restoring MainWindow also restore AdditionalWindow
  • Closing MainWindow closes AdditionalWindow , but closing AdditionalWindow does not close MainWindow
  • AdditioanlWindow will never be "lost" under MainWindow , i.e. AddditionalWindow always displayed above MainWindow in z-order if you used Show() to display it (quite useful!)

It should be noted that if you have this relationship, the Closing event on AdditionalWindow not raised, so you will have to manually OwnedWindows over the OwnedWindows collection. for example, Create a way to invoke each window using a new interface or base class method.

  private void MainWindow_OnClosing(object sender, CancelEventArgs e) { foreach (var window in OwnedWindows) { var win = window as ICanCancelClosing; // a new interface you have to create e.Cancel |= win.DoYouWantToCancelClosing(); } } 
0
Jul 27 '16 at 15:02
source share



All Articles