When the user closes my application I'd like to be able to prompt them with a confirmation if they have unsaved changes, and cancel the application's closing if they indicate to do so. The Application's Exit event does not allow cancellation. Is there any way to do this?
Asked
Active
Viewed 2,016 times
1 Answers
3
Catch the Closing event of the MainWindow instead:
App.Current.MainWindow.Closing += MainWindow_Closing;
Then you can set the Cancel property to true in the event handler if necessary:
private void MainWindow_Closing(object sender, System.ComponentModel.ClosingEventArgs e)
{
e.Cancel = true;
}
Hope this helps...
Chris

Chris Anderson
- 8,279
- 2
- 20
- 18
-
I also found this question which explained why this wasn't working for me. http://stackoverflow.com/questions/3591446/mainwindow-closing-event-not-always-raised-in-silverlight-4-oob-app – Matt Casto Oct 11 '10 at 12:38
-
Hi Chris... I always seem to get the oddities in SL ;) Anyway - this does not work for me - the event never fires (I have tried it in App.cs and MainPage. I looked at the link above (Matt) and tried an IApplicationService but theres no way to cancel the event. So basically I cannot ask the user if they want to cancel when they click on the Close button when running OOB (trusted)... – Rodney Mar 28 '11 at 01:38
-
Hi Rodney. Hmm, not sure. Do you possibly have any code demonstrating the problem that you can send me to take a look at? – Chris Anderson Mar 29 '11 at 05:45
-
Thanks Chris, I found the problem - it is a known issue with opening/closing ChildWindows and the Closing event - I posted a link on this question - http://stackoverflow.com/questions/3591446/mainwindow-closing-event-not-always-raised-in-silverlight-4-oob-app – Rodney Apr 04 '11 at 04:39