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
This code helps you -
In App.xaml.cs
...
using Windows.ApplicationModel;
...
public App()
{
InitializeComponent();
this.Suspending += OnSuspending;
}
...
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//Add your logic here, if any
deferral.Complete();
}
Thanks!!!
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:
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
Manifest:
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="confirmAppClose"/>
</Capabilities>
It needs extra approval when submitting to the store. But then will fire the CloseRequested
event on a SystemNavigationManagerPreview instance.
Code:
public MainPage()
{
this.InitializeComponent();
SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += this.OnCloseRequest;
}
private void OnCloseRequest(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
{
if (!saved) { e.Handled = true; SomePromptFunction(); }
}
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).
From official page about app lifecycle:
There's no special event to indicate that the user closed the app.
Closed-by-user behavior: If your app needs to do something different when it is closed by the user than when it is closed by Windows, you can use the activation event handler to determine whether the app was terminated by the user or by Windows.
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.
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.