WPF hides window in Closing event prevents application from shutting down

Another simple question.

I use a window in WPF as a child window, where I prefer the "X" button to hide the window instead of closing it. For this I have:

private void Window_Closing(object sender, CancelEventArgs e) {
   this.Hide();
   e.Cancel = true;
}

The problem is that when you close the parent window, it never closes and does not save the application.

Is there a clean way to handle this? I was thinking of adding a Kill flag to all of my user controls (windows):

public bool KillMe;

private void Window_Loaded(object sender, RoutedEventArgs e){
   KillMe = false;
}

private void Window_Closing(object sender, CancelEventArgs e) {
   this.Hide();
   if (!KillMe) e.Cancel = true;
}

Then in MainWindow_Closing () I need to set all the KillMe flags of the window to true.

Any better way than creating extra flags and forgetting to set them before closing?

+3
source share
3 answers

Shutdown "parent's"... .

Window.Closing:

Shutdown, Closing . , , .

+7

AppGeneral . , AppGeneral.IsClosing static bool true. , :

private void Window_Closing(object sender, CancelEventArgs e) {
if (!AppGeneral.IsClosing)   
   { 
     this.Hide();
     e.Cancel = true;
   }
}

, ( , :)) Process.GetCurrentProcess().Kill();

+2

You have to use

Application.Current.Shutdown();

inside the method of closing the main window.

This should cancel the cancellation of any substring!

+1
source

Source: https://habr.com/ru/post/1786453/


All Articles