UWP on desktop closed by top X button - no event

2019-01-19 11:28发布

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?

3条回答
相关推荐>>
2楼-- · 2019-01-19 11:50

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.

查看更多
在下西门庆
3楼-- · 2019-01-19 11:55

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.

查看更多
倾城 Initia
4楼-- · 2019-01-19 11:59

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

查看更多
登录 后发表回答