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
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 with Suspending
you can (more or less) safely determine that desktop app is closing. Good thing is, VisibilityChanged
fired before Suspending
and you can save it's value and check it in OnSuspending
handler, after which decide if you need to do any cleaning up or other work.