如何检测 UWP 的退出 [英] How to detect exit for UWP

查看:30
本文介绍了如何检测 UWP 的退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检测当前的 UWP 要退出吗?(被用户关闭或终止进程)

Is there any way to detect current UWP going to exit? (close by user or kill the process)

我想向服务器发送一些关于应用程序将断开连接的请求,还想在退出之前保存一些数据.

I want to send some request to the server about the application will disconnect, also want to save some data before it exit.

推荐答案

没有办法检测这种情况或阻止用户终止您的应用程序.如果您想在退出前保存状态或执行某些操作,请使用 暂停事件:

There is no way to detect such case or to prevent user from terminating your app. If you want to save the state or do something before exit, use Suspending event:

Suspending 事件是您的应用在终止(如果发生)之前将收到的唯一指示.因此,您应该存储足够的会话状态(例如当前正在阅读的文章或当前的电影播放位置)以在激活期间重新创建完全相同的体验.内容创建应用程序的指导原则是尽早并经常保存用户的工作,但也要在暂停期间提交最后一次保存.在暂停之前保存数据很有用,因为 Suspending 事件处理程序只有 5 秒的时间来完成其操作.

The Suspending event is the only indication your app will receive prior to termination (if it happens). Because of this, you should store enough session state (such as the current article being read or the current movie playback position) to recreate the exact same experience during activation. The guidance for content creation apps is to save a user’s work early and often but also commit one final save during Suspending. Saving data prior to suspension is useful because the Suspending event handler has only 5 seconds to complete its operation.

记住时间有限.

事实上,还有两个其他事件会被触发(在移动情况下,当用户按住后退按钮并转到任务切换器时):Window.VisibilityChangedWindows.Activated,但是当用户更改应用程序、显示提示等时它们也会被触发 - 并且无法区分这些情况.

In fact there are two other events that will be fired (in mobile case, when user holds back button and goes to task switcher): Window.VisibilityChanged and Windows.Activated, but they are also fired when user change the app, show prompt and so on - and there is no way to distinguish those situations.

这篇关于如何检测 UWP 的退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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