当UWP应用程序被系统关闭时,如何获得终止或终止事件



在设置中更改联系人访问权限将终止UWP应用程序。

系统关闭应用程序时,如何获取terminatingterminated事件?

在设置中更改联系人访问权限将终止UWP应用程序。

@Peter Torr-MSFT是正确的。这种行为是故意的。当您更改隐私设置时,它只是被迫使用新的隐私设置重新启动。但目前UWP应用程序无法通过应用程序容器外的控制器进行重新启动,因此它已被终止。

但在这种情况下,应用程序应该得到通知或必须重新启动。

您可以在WPDev UserVoice上提交"功能请求"。

App.xaml.cs文件中的App类的构造函数中订阅UnhandledExceptionSuspending事件

public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
this.UnhandledException += App_UnhandledException;
}

每当应用程序中发生异常时,此事件都会触发

private async void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
{
// do your job
e.Handled = true;
}

您还可以设置异常trueHandled属性,以防止应用程序崩溃并以错误的方式关闭。

每当您的应用程序执行被暂停时,此事件就会触发

/// <summary>
/// Invoked when application execution is being suspended.  Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
deferral.Complete();
}

最新更新