I have an screen in my app where a user enters data. When they leave the page the entered data is not saved, how can I implement a confirmation mechanism if the user has not saved the data. I am using MVVM as a design pattern.
Thanks in advance
I have an screen in my app where a user enters data. When they leave the page the entered data is not saved, how can I implement a confirmation mechanism if the user has not saved the data. I am using MVVM as a design pattern.
Thanks in advance
You have to consider two things:
first - the User leaves your Page with Back Button - he wants to navigate back or exit the App - in this case you should show some kind of prompt with Save/No buttons. You must do it before Frame.GoBack()
executes - depending on where you navigation back is performed (App.xaml.cs, NavigationHelper, other), you will have to nadle it differentially. The main idea is to invoke your eventhandler with prompt, before the one with navigation, and set e.Handeled = true
(you will also have to ensure that the eventhandler with navigation check if e.Handeled != true
). As for subscribing before any other eventhandlers, you may have a look at this code snippet
second - the User leaves your Page with Start/Search/Hold Back buttons - in this way your App will be Suspended and you cannot stop it. What you can do in this situation - subscribe to the Suspending event and save the state of what user has entered, then bring it back on Resuming event.