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.