Asked by:

[UWP][VB]Prevent a UWP App From Closing

Question

I have an app in which the user edits a text document. If the user attempts to close the app before saving their changes, I want to open a Popup confirming that they really want to close the app. The two things I need to know are how to detect that the
user is trying to close the app and how to prevent the app from closing if the user decides they don't want to close it. How can I do this? Thanks.

All replies

You shouldn't rely on the user explicitly saving to prevent data loss. Best practices are to automatically save the file and keep undo info so the user can revert or to automatically save a draft so you can restore to where they were when they return to
the app.

In general, telling the user "do you really want to do that?" when they've just tried to do something like closing the app is a poor user interface, and modern application design recommends following the user's wishes and having the app implicitly
protect against irrevocable mistakes.

The Creators Update added a
SystemNavigationManagerPreview.CloseRequested event, but this will only trigger if the user invokes the close button explicitly. There are a number of situations which won't trigger it (see the Remarks in the docs) so you'll need to persist the user data
for suspension and be able to restore it anyway.