WPF Hide on Close?

How to do it in wpf

Vb.net

Private Sub FrmSettings_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing e.Cancel = (e.CloseReason = Forms.CloseReason.UserClosing) Me.Hide() End Sub 

WITH#

 private void FrmSettings_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) { e.Cancel = (e.CloseReason == Forms.CloseReason.UserClosing); this.Hide(); } 

like wpf Close event just gives me e.Cancel and no closereason: (

+4
source share
3 answers

In the WPF implementation, there is no equivalent by default. You can use the window hook to get the reason.

The following post details how to do this: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/549a4bbb-e77b-4c5a-b724-07996774c60a/

+5
source

I'm not sure I understand that WinForms approach solves.

Isn't it better to always do this:

 Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs) e.Cancel = True Me.Hide() End Sub 

And then install this in your application?

 Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose 

This way, whenever your child windows close, you save them for faster display later, but your application still shuts down when the main window closes (i.e. exit, shutdown, etc.).

+5
source

I would like to thank Bob King for his hint and make an addition to his code as C # WPF. It works for me. My application is a tray icon by type. In the WPF XAML form code behind:

 protected override void OnInitialized(EventArgs e) { base.OnInitialized(e); Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose; } private bool m_isExplicitClose = false;// Indicate if it is an explicit form close request from the user. protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { base.OnClosing(e); if (m_isExplicitClose == false)//NOT a user close request? ... then hide { e.Cancel = true; this.Hide(); } } private void OnTaskBarMenuItemExitClick(object sender, RoutedEventArgs e) { m_isExplicitClose = true;//Set this to unclock the Minimize on close this.Close(); } 
+5
source

All Articles