Application.Current.Exit() 和 OnSuspendingEvent
Application.Current.Exit() and OnSuspendingEvent
在 Universal Windows 10 C#/Xaml 应用程序中,当我通过 Alt+F4 关闭我的应用程序时 OnSuspending
事件被触发。虽然,当我调用 Application.Current.Exit()
时情况并非如此,尽管文档指出只要应用程序即将终止就会触发此事件。
为什么会这样?看起来不一致的行为导致我的应用程序出现错误。
- Microsoft does not recommend 从代码中退出应用程序:
Use this method to provide UI that enables users to exit your app. Normally, however, you should not provide this UI because the system automatically manages app lifetime and terminates suspended apps as needed to free resources.
- Application.Exit 不会暂停应用程序,而是将其关闭 - 这就是为什么不调用 OnSusspending 中的代码的原因
- 解决方法:在您的 App.cs 中添加
a) using Windows.ApplicationModel.Core;
b) 在 App 构造函数中添加 CoreApplication.Exiting += CoreApplication_Exiting;
c) 编写CoreApplication_Exiting方法
private void CoreApplication_Exiting(object sender, object e)
{
//HERE your code
}
当应用程序终止时,它会一直运行。
Microsoft 的这张官方图片说明了 Windows 通用平台应用程序的应用程序生命周期:
documentation of this lifecycle说明:
After an app has been closed by the user, it's first suspended and then terminated, and enters the NotRunning state.
此外,Microsoft 建议不要 以编程方式关闭应用程序!
来自 "Guidelines for app suspend and resume":
Don't include Close buttons or offer users other ways to terminate your app in its UI. Users should feel confident that the system is managing their apps for them. The system can terminate apps automatically to ensure the best system performance and reliability, and users can choose to close apps using gestures on Windows or through the Task Switcher on Windows Phone.
但是如果你...
close an app programmatically, the system treats it as an app crash.
因此,您的应用将从 运行 状态直接进入 Not运行 状态.
如果您
处理 CoreApplication
class.
的 Exiting
事件
OnSuspending
在应用程序暂停时调用。它与终止无关。该文档指出,当用户使用 alt+f4 关闭应用程序时,它也会被调用……但这只是一件好事。
对于终止,没有触发任何事件。这就是为什么您应该在暂停时保存状态。
例如,如果应用程序崩溃,OnSuspending
也不会被调用。
如果你看这个:https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.exit
它说
“Normally, however, you should not provide this UI because the system
automatically manages app lifetime and terminates suspended apps as
needed to free resources”
意思是当您使用此方法终止您的应用程序时,不会调用 OnSuspend
。
处理案例 1 之间的区别:挂起-> 由系统终止(例如,当您的应用程序最小化且此后不再使用时)或由 Alt+F4 关闭和案例 2:应用程序崩溃或由 Application.Current.Exit()
在 OnLaunched
方法中使用 LaunchActivatedEventArgs
上的 PreviousExecutionState
(或者如果使用 OnActivated
上的 IActivatedEventArgs
)。
在 Universal Windows 10 C#/Xaml 应用程序中,当我通过 Alt+F4 关闭我的应用程序时 OnSuspending
事件被触发。虽然,当我调用 Application.Current.Exit()
时情况并非如此,尽管文档指出只要应用程序即将终止就会触发此事件。
为什么会这样?看起来不一致的行为导致我的应用程序出现错误。
- Microsoft does not recommend 从代码中退出应用程序:
Use this method to provide UI that enables users to exit your app. Normally, however, you should not provide this UI because the system automatically manages app lifetime and terminates suspended apps as needed to free resources.
- Application.Exit 不会暂停应用程序,而是将其关闭 - 这就是为什么不调用 OnSusspending 中的代码的原因
- 解决方法:在您的 App.cs 中添加
a) using Windows.ApplicationModel.Core;
b) 在 App 构造函数中添加 CoreApplication.Exiting += CoreApplication_Exiting;
c) 编写CoreApplication_Exiting方法
private void CoreApplication_Exiting(object sender, object e)
{
//HERE your code
}
当应用程序终止时,它会一直运行。
Microsoft 的这张官方图片说明了 Windows 通用平台应用程序的应用程序生命周期:
documentation of this lifecycle说明:
After an app has been closed by the user, it's first suspended and then terminated, and enters the NotRunning state.
此外,Microsoft 建议不要 以编程方式关闭应用程序!
来自 "Guidelines for app suspend and resume":
Don't include Close buttons or offer users other ways to terminate your app in its UI. Users should feel confident that the system is managing their apps for them. The system can terminate apps automatically to ensure the best system performance and reliability, and users can choose to close apps using gestures on Windows or through the Task Switcher on Windows Phone.
但是如果你...
close an app programmatically, the system treats it as an app crash.
因此,您的应用将从 运行 状态直接进入 Not运行 状态.
如果您
处理 CoreApplication
class.
Exiting
事件
OnSuspending
在应用程序暂停时调用。它与终止无关。该文档指出,当用户使用 alt+f4 关闭应用程序时,它也会被调用……但这只是一件好事。
对于终止,没有触发任何事件。这就是为什么您应该在暂停时保存状态。
例如,如果应用程序崩溃,OnSuspending
也不会被调用。
如果你看这个:https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.exit 它说
“Normally, however, you should not provide this UI because the system automatically manages app lifetime and terminates suspended apps as needed to free resources”
意思是当您使用此方法终止您的应用程序时,不会调用 OnSuspend
。
处理案例 1 之间的区别:挂起-> 由系统终止(例如,当您的应用程序最小化且此后不再使用时)或由 Alt+F4 关闭和案例 2:应用程序崩溃或由 Application.Current.Exit()
在 OnLaunched
方法中使用 LaunchActivatedEventArgs
上的 PreviousExecutionState
(或者如果使用 OnActivated
上的 IActivatedEventArgs
)。