简体   繁体   English

发布模式 UWP 应用程序在平板电脑模式下和 OnSuspended 中的代码崩溃

[英]Release mode UWP app crashes when in tablet mode and with code in OnSuspended

We have a Xamarin UWP app that needs to be logged out whenever a user minimizes or clicks away from the window.我们有一个 Xamarin UWP 应用程序,每当用户最小化或单击远离窗口时,都需要注销该应用程序。

In my App.xaml.cs I have registered an event handler for the Suspending event.在我的 App.xaml.cs 中,我为 Suspending 事件注册了一个事件处理程序。 I then put our logout code in this event handler like so:然后我将我们的注销代码放在这个事件处理程序中,如下所示:

private void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.GetDeferral();
    AppBackgrounded();
    deferral.Complete();
}

This AppBackgrounded() method looks like this:这个 AppBackgrounded() 方法看起来像这样:

void AppBackgrounded()
{
    if (!_isInBackgroundMode)
    {
        _isInBackgroundMode = true;
        if (UserSetPin)
        {
            PinPage passcodePin = new PinPage();
            Navigation.PushModalAsync(New NavigationPage(passcodePin), false);
        }
        else
        {
            App.Logout(null, true, true);
        }
        // clears the pasteboard so data can't be copied from this app into other apps 
        Clipboard.Clear();
    }
}

We also have a AppLeavingBackground method that we use to restore the app when the user returns, but the app does not crash when returning.我们还有一个 AppLeavingBackground 方法,用于在用户返回时恢复应用程序,但应用程序在返回时不会崩溃。 It only crashes when running the OnSuspended method.它仅在运行 OnSuspended 方法时崩溃。

This crash only occurs when此崩溃仅发生在

  1. The App is built for release and该应用程序专为发布和
  2. The device is in tablet mode设备处于平板电脑模式

When in tablet mode, if you press the Task View button and navigate to another application the UWP app freezes trying to run through this code.在平板电脑模式下,如果您按下“任务视图”按钮并导航到另一个应用程序,UWP 应用会在尝试运行此代码时冻结。 If you try to return to the app, it will immediately exit.如果您尝试返回该应用程序,它将立即退出。

I have tried to make the navigation to the other pages async and the app will then crash even when its not in tablet mode.我试图使导航到其他页面异步,即使应用程序不在平板电脑模式下也会崩溃。 I have also tried to put this logic in AppEnteredBackground and it still occurs.我也尝试将此逻辑放在 AppEnteredBackground 中,但它仍然发生。

This is hard to debug since it only occurs in release mode.这很难调试,因为它只发生在发布模式下。 Any ideas?有任何想法吗?

In my case it was the Clipboard.Clear() function that was crashing the application.就我而言,是 Clipboard.Clear() 函数导致应用程序崩溃。 For those encountering similar issues, checkout the other answers as they all provide great points.对于那些遇到类似问题的人,请查看其他答案,因为它们都提供了很好的观点。

As a side note, I also found that using async code in these events was crashing my application.作为旁注,我还发现在这些事件中使用异步代码会导致我的应用程序崩溃。 I'm not sure why since I was using the deferral, but since it's working i'm not going to pursue it further.我不知道为什么,因为我使用了延期,但既然它正在起作用,我不打算进一步追求它。

I cannot say that I have read exactly the documentation that says that navigating the pages in suspending will result in a crash, but it is clear that this is the wrong place to do it.我不能说我已经完全阅读了说明在暂停状态下导航页面会导致崩溃的文档,但很明显,这是错误的地方。

The suspending is not used to prepare your app to be opened again, the whole reason for this lifecycle event is that you need to prepare your app for not being open again, which means saving some data that may eventually get lost.暂停不是用来准备你的应用程序再次打开,这个生命周期事件的全部原因是你需要准备你的应用程序不再打开,这意味着保存一些最终可能会丢失的数据。 Preparing your app to be opened again is done in resuming.准备再次打开您的应用程序是在恢复中完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UWP - MessageDialog 在 Windows Phone 和平板电脑模式下使应用程序崩溃 - UWP - MessageDialog crashes the app on Windows Phone and tablet mode 在发布模式下构建uwp应用程序时出错 - Error when building uwp app in Release mode 不处于调试模式时,UWP应用程序崩溃 - UWP app crashes when not in debugging mode UWP-WebAuthenticationBroker在发布模式下在设备上崩溃 - UWP - WebAuthenticationBroker crashes on device in release mode Xamarin Monogame 应用程序在启动时在发布模式下崩溃 - Xamarin Monogame app crashes in release mode on startup UWP XAMARIN在RELEASE模式下崩溃(但在调试中运行良好) - UWP XAMARIN crashes in RELEASE mode (but good working in debug) 应用程序在发布模式下崩溃,而不是在调试模式下崩溃 - Xamarin Forms - App crashes in release mode and not in debug mode - Xamarin Forms 在调试模式下运行的同一Android应用程序在发布模式下崩溃 - Same Android app who runs on Debug mode, crashes on release mode HttpListener在发布模式下崩溃 - HttpListener Crashes in Release mode 错误MCG0004:InternalAssert断言在发布模式下构建UWP应用程序时失败 - Error MCG0004:InternalAssert Assert Failed when building UWP app in release mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM