处理应用程序退出 [英] Handle application exit

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

问题描述

我想在用户使用 Alt-F4 或向下滑动手势退出应用程序时运行一些清理代码(例如取消注册预定通知).有没有办法在 WinJS 中处理应用程序退出?我已经阅读了 WinJS.Application 对象的文档,但没有看到任何处理用户退出的方法.

I want to run some cleanup code(like unregistering scheduled notifications) when a user quits the application by using the Alt-F4 or swipe down gesture. Is there any way to handle an application exit in WinJS? I've read the docs for the WinJS.Application object but don't see any methods to handle user exits.

推荐答案

没有表示应用正在关闭的特殊事件:

没有特殊事件表明用户已关闭应用.用户关闭应用后,它会暂停并终止,并在大约 10 秒内进入 NotRunning 状态.如果应用程序已为 Suspending | 注册了事件处理程序suspending 事件,在应用程序暂停时调用.您可以使用此事件处理程序将相关应用程序和用户数据保存到持久存储中.

There's no special event to indicate that the user has closed an app. After an app has been closed by the user, it's suspended and terminated, entering the NotRunning state within about 10 seconds. If an app has registered an event handler for the Suspending | suspending event, it is called when the app is suspended. You can use this event handler to save relevant application and user data to persistent storage.

因此,您需要处理挂起/恢复.处理挂起的详细信息在这里,但这里有一个总结:

So you'll want to handle a suspend/resume instead. The gory details for handling a suspend are here, but here's a summary:

  1. 注册 检查点 事件,该事件将告诉您的应用它正在暂停.
  2. 保存您需要在该事件的事件处理程序中保存的任何数据.
  3. 也在事件处理程序中释放资源、暂停通知等.
  1. Register for the checkpoint event that will tell your app that it's being suspended.
  2. Save whatever data you need to save in the event handler for that event.
  3. Release resources, suspend notifications, etc. in the event handler as well.

在恢复时,您可以使用 ApplicationExecutionState 枚举.这可能与您相关,也可能不相关,因为似乎没有办法区分 checkpoint 事件被触发的原因,而您唯一的选择是无论如何都将状态保存在事件处理程序中它发生了.

On resume, you can check if the app was closed by the user using the ApplicationExecutionState enum. That may or may not be relevant to you, since there doesn't seem to be a way to differentiate why checkpoint event was fired and your only option is to save your state in the event handler no matter why it happened.

还有其他暂停/恢复指南此处,您可能会找到这个示例应用很有帮助.

There are additional suspend/resume guidelines here, and you may find this sample app helpful.

这篇关于处理应用程序退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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