I wanted to give WPF a try and was creating a timer application which will pop up a window once a while. But what I wanted was for the application to keep running in the tray even when its closed. The following code does NOT work in WPF for some reason:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
Window.Visibility = !Window.Visibility;
}
In order to hide a closing window in WPF, this is what needs to be done:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//Do some stuff here
//Hide Window
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, (DispatcherOperationCallback)delegate(object o)
{
Hide();
return null;
}, null);
//Do not close application
e.Cancel = true;
}
Hi, Balaji.
The code works fine:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
Visibility = Visibility.Collapsed;
}
Invoking is only required if multithreading model is used.
Another problem is that you never close your application, because with such code you can not close the window.
This also works. Thanks for the code = )
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
Hi all,
you also can set the Application.ShutdownMode Property to OnExplicitShutdown instead of OnMainWindowClose. The App will run until you call Shutdown explecit.
See: http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx