开发者

Changing Wpf window appearance on Window_Closing

When the user clicks the x to close the application window I want to pop up a message 开发者_如何学Pythonsaying "Shutting Down, please wait" and greyout or disable the rest of the window controls.

This is because my application shutdown and cleanup takes a little while, about 20 seconds.

However, wpf commands in the Window_Closing event handler never seem to do anything. I have a tranparent grey-filled border which overlays the window controls and I want to change the visibility from collapsed to visible. The code is called but nothing changes.

It is as if the request is queued but is not called because the window is closing.

Is there some way to flush this request out?

Edit:

Try this code:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    _DisableBorder.Visibility = System.Windows.Visibility.Visible; 
    Thread.Sleep(2000); 
}

The app will close after 2 sec, but the border will never show. If you add e.Cancel = true, then the border will show after 2 sec.


You should make a trick. Within the "Closing" handler, you should cancel the closing, then show the popup (as modeless window), then disable everything on the main window. When the process is over, you can close both the popup and the main windows programmatically.

I'd not perform anything time-consuming within the closing handler.

Hope it helps. Cheers


Calling DoEvents from System.Windows.Forms.Application seems to do the trick..

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   _DisableBorder.Visibility = System.Windows.Visibility.Visible; 
   System.Windows.Forms.Application.DoEvents();
   Thread.Sleep(2000);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜