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

5

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

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


你的意思是想要引起终止的事件?还是执行终止的事件? - Jaskier
任何可以告诉应用程序即将终止或已经终止的事件,都需要进行清理工作。 - Vikas Sardana
你是在说 app.xaml.cs 文件吗? - Shubham Sahu
我非常确定这个进程只是出于隐私原因立即被终止了。 - Peter Torr - MSFT
@PeterTorr-MSFT 但在这种情况下,应用程序应该被通知或必须重新启动。 - Vikas Sardana
2个回答

2
在设置中更改联系人访问权限会终止UWP应用程序。
@Peter Torr - MSFT是正确的。这种行为是设计上的。当您更改隐私设置时,它只是强制使用新的隐私设置重新启动。但目前,UWP应用程序无法通过应用程序容器外部的控制器进行重启,因此它已被终止。
但在这种情况下,应该通知应用程序或必须重新启动。
您可以在WPDev UserVoice上提交“功能请求”。

0

App.xaml.cs文件的构造函数中订阅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;
    }

你还可以设置异常的 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();
    }

1
尝试使用OnSuspending和UnhandledException,但在这种情况下都没有触发。 - Vikas Sardana
你设置了这个吗 => e.Handled = true; ? - Parsa Karami
在这种情况下,UnhandledException事件未触发。 - Vikas Sardana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接