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

C# 当UWP应用程序被系统关闭时,如何获取终止或终止事件?,c#,.net,events,uwp,windows-10,C#,.net,Events,Uwp,Windows 10,在设置中更改联系人访问将终止UWP应用程序 当系统关闭应用程序时,如何获取终止或终止事件 在App.xaml.cs文件中的App类的构造函数中订阅UnhandledException和Suspending事件 public App() { this.InitializeComponent(); this.Suspending += OnSuspending; this.UnhandledException += App_Unhand

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


当系统关闭应用程序时,如何获取
终止
终止
事件

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

    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;
    }
您还可以设置异常的
Handled
属性
true
,以防止应用程序崩溃并以不良方式关闭

每当应用程序执行被挂起时,都会触发此事件

    /// <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();
    }
//
///在挂起应用程序执行时调用。应用程序状态已保存
///不知道应用程序是否会随内容一起终止或恢复
///记忆仍然完好无损。
/// 
///挂起请求的源。
///有关暂停请求的详细信息。
暂停时的专用异步void(对象发送方,SuspendingEventArgs e)
{
var deleral=e.SuspendingOperation.getdeleral();
//TODO:保存应用程序状态并停止任何后台活动
延迟。完成();
}
在设置中更改联系人访问将终止UWP应用程序

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

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


您可以在上提交“功能请求”

你的意思是你想要导致终止的事件吗?或终止的事件?任何可以说明应用程序已终止或将终止的事件,只是为了清理这些东西。你是在说app.xaml.cs吗?非常肯定,由于隐私原因,进程会立即被终止。@PeterTorr MSFT,但在这种情况下,应用程序应该得到通知或必须重新启动。在这种情况下,Twed-OnSuspending和UnhandledException都不会触发。是否设置此=>
e.Handled=true?此场景中未触发的未处理异常事件