Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP on desktop closed by top X button - no event

Tags:

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?

like image 616
tomab Avatar asked Jan 28 '16 08:01

tomab


People also ask

How do I fix UWP app not opening?

Run the Troubleshooter for Windows Store Apps The Windows Store Apps troubleshooter is one included with Microsoft's flagship OS to fix errors that pertain to UWP apps. As this is a UWP issue, running that troubleshooter might resolve the “This app can't open” issue for some users at least.

How do I exit UWP app?

TryConsolidateAsync() to close the app. The obvious benefit of this method is app is closed just as you will do by pressing close button in titlebar, the closing is graceful, animation is the same and app is suspended instead of abruptly exiting.

How do I close a page in UWP?

Executing ApplicationView. GetForCurrentView(). TryConsolidateAsync() in main window, closes it properly as if you have closed it by clicking close button in title bar.


2 Answers

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).

like image 107
Michael Hawker - MSFT Avatar answered Oct 08 '22 20:10

Michael Hawker - MSFT


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.

like image 22
tomab Avatar answered Oct 08 '22 18:10

tomab