An UWP app which runs on desktop can be closed from the top X button but it doesn't have any event for it. It is known that on phones and tablets an app should rely on Suspending
event, no matter how it's triggered and then the app should rely on ApplicationExecutionState
.
However, here is a (maybe) common scenario: on phones the Suspending
event suffice and in case a Voip call is going on it will be operated by OS after the app is suspended. On desktop the close button is expected, by user, to completely close the app. So if a call is on going it should be hanged up and certain resources should be released.
How can I know when the user clicked the "close" button if (and only if) the UWP app is running on desktop?
I deleted my original answer with Window.Current.Closed event, because it doesn't seem to work if you have only one Window instance. There is also
CoreApplication.Exiting
, but judging by this topic it doesn't work either. Seems to be known issue which might be fixed in future. At the end, it appears there is no clear way to determine when the app is getting closed, only when it's suspended.However,
Window.Current
does fire VisibilityChanged event when app is closed. Problem is, it's also fired when app is minimized and maybe in some other cases. But I think in combination withSuspending
you can (more or less) safely determine that desktop app is closing. Good thing is,VisibilityChanged
fired beforeSuspending
and you can save it's value and check it inOnSuspending
handler, after which decide if you need to do any cleaning up or other work.From official page about app lifecycle:
So according to this there is no (clear) way to know if the user closed the app before the app is closed but only after it's restarted. Too bad.
A restricted capability
confirmAppClose
was added in Windows 10 version 1703 (build 10.0.15063) in order to provide apps the ability to intercept window closing.Manifest namespace:
Manifest:
It needs extra approval when submitting to the store. But then will fire the
CloseRequested
event on a SystemNavigationManagerPreview instance.Code:
You can get a deferral to do a bit of work here (save or prompt), or you can set
Handled
to true in order to stop the window from closing (user cancelled prompt).