WPF Hide on Close?
Solution 1
There is not an equivalent in the default implementation of WPF. You can use a windows hook to get the reason though.
The following post details how to do this: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/549a4bbb-e77b-4c5a-b724-07996774c60a/
Solution 2
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 a 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();
}
Solution 3
I'm not sure I understand what the 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 set this in your Application?
Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose
This way, whenever your child windows close, you keep them around for faster display later, but your app still shutsdown when the main window closes (i.e. Exit, Shutdown, etc).
Comments
-
Raven almost 2 years
How do i do this 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
c#
private void FrmSettings_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) { e.Cancel = (e.CloseReason == Forms.CloseReason.UserClosing); this.Hide(); }
as wpf's Close event just gives me e.Cancel and no closereason :(
-
Raven about 15 yearsHow ugly that there is nothing built in :(
-
Poma about 12 yearsThis way it will not close through task manager or at windows logoff. Also in this case you can't save application data on exit.
-
Bob King about 12 yearsI'm not sure if I get what you're saying @Poma... you can override the Applications OnExit method to handle saving data on exit, etc.
-
Poma about 12 yearsIf I shutdown windows Applications OnExit method will never be called because cancelling close event will prevent application from closing. Eventually windows will just kill this process.
-
Poma about 12 yearsThis way it will crash when closed through task manager or at windows logoff
-
Bob King about 12 yearsThat's definitely not true at all. You have some additional changes outside of this base case that are causing your app to not exit cleanly. Are you marking a main window correctly? Is your main window not visible at shutdown. My solution works in our application reliable and cleanly.