桌面上的UWP由顶部的X按钮关闭-无事件 [英] UWP on desktop closed by top X button - no event

查看:190
本文介绍了桌面上的UWP由顶部的X按钮关闭-无事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以通过顶部的X按钮关闭在桌面上运行的UWP应用,但没有任何事件。众所周知,在手机和平​​板电脑上,应用程序都应依赖 Suspending 事件,无论它是如何触发的,然后应用程序都应依赖 ApplicationExecutionState

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.

如果(且仅当)UWP应用程序在桌面上运行时,我如何知道用户单击关闭按钮的时间?

How can I know when the user clicked the "close" button if (and only if) the UWP app is running on desktop?

推荐答案

A 受限功能 confirmAppClose 已在Windows 10版本1703(内部版本10.0.15063)中添加为了使应用能够拦截窗口关闭。

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.

清单名称空间:

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"

清单:

<Capabilities> 
  <Capability Name="internetClient" /> 
  <rescap:Capability Name="confirmAppClose"/> 
</Capabilities> 

提交到商店时需要额外的批准。但是随后将在CloseRequested 事件.preview.systemnavigationmanagerpreview rel = noreferrer> SystemNavigationManagerPreview 实例。

It needs extra approval when submitting to the store. But then will fire the CloseRequested event on a SystemNavigationManagerPreview instance.

代码:

    public MainPage()
    {
        this.InitializeComponent();
        SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += this.OnCloseRequest;
    }

    private void OnCloseRequest(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
    {
        if (!saved) { e.Handled = true; SomePromptFunction(); }
    }

您可以延期在这里做一些工作(保存或提示),也可以将 Handled 设置为true,以阻止窗口关闭(用户取消的提示)。

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

这篇关于桌面上的UWP由顶部的X按钮关闭-无事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆