views:

497

answers:

3

Here's a question for all you Silverlight guys.

In the old days for WinForms, if your user was creating/editing some information in a DialogBox, it was easy to detect the Window closing and if the data was dirty, ask if they wanted to save.

My question is, how do you approach this scenario in Silverlight where everything seems to be done in UserControls, which have no obvious way of knowing when the page is closing, ie switching to another page within the Silverlight app would simply open a different UserControl without checking the previous UserControl needed saving.

There must be some standard way of achieving this?

+2  A: 

You can handle the Application.Exit event to execute code on application shut down. Be warned that at this time the networking stack has already shut down so you can't send any messages from the Application.Exit. You cannot cancel the event.

Michael S. Scherotter
+1  A: 

+1 Michael. It is a current limitation of SL. You can however persist to isolated storage from the app exit event, would it be feasible in your app to check if dirty, and if so save to iso storage. Then next time your app loads you could load the dirty data from iso storage ready to carry on editing..?

Jason Roberts
Hi Jason, do you know if this has been rectified in Silverlight 4? Surely it's a backward step if we can't stop our users foolishly clicking on the wrong button and throwing away their changes without being prompted.
Mitch
Hi Mitch, sorry don't know - haven't had chance to look at any beta 4 stuff yet - we could spend are whole life in betas!!The other option that i've seen written is to have a timer in the background which autosaves (to a web service) every few mins - again not ideal...
Jason Roberts
Thanks for your feedback Jason, I hope MS make Silverlight 4 more suitable for LOB apps. Surely it's not that difficult to detect when a Usercontrol is being closed!?
Mitch
+2  A: 

If you need to ask the user a question about whether to save or not then you will need to handle this in the browser the onbeforeunload (whilst not a standard event it is supported by those browsers on which Silverlight is officially supported). It may be possible to set up a little javascript to call back into your application when onbeforeunload fires.

That said I'm not a fan of the "Are you sure, you want to lose all that work you've just done" type of question. "Of course I don't want lose it, save it already, as long as I can undo it later if necessary I'm happy".

AnthonyWJones
Hi Anthony, thanks for the reply. Surely if you have a way of "saving it already", you also would have a way of asking "do you want to 'save it already'". I'm not so fussed if the user clicks on the app close button, more if the user simply selects a different page from the app menu without saving.
Mitch
My point was not about whether you would have a __way__ of asking, my point was I would prefer programs __not__ to ask at all.
AnthonyWJones
I agree, but the last app I wrote like that, the first comments I had back from the users was why does it let me exit without saving! I think users need to be saved from themselves!
Mitch
@Mitch: Yes thats a good point, we've trained our users into expecting this dialog and they become unnerved by an app which doesn't conform to what they've grown accustomed. However Silverlight presents use with an opportunity to give users a different enough experience that it should be easier for them to accept this difference also. Still access the `onbeforeload` browser window event should allow you to maintain this expectation.
AnthonyWJones
Thanks for your comments Anthony, I'll check out onBeforeLoad and see how I get on.
Mitch