C# 当处于平板电脑模式且代码处于OnSuspended时,发布模式UWP应用程序崩溃
我们有一个Xamarin UWP应用程序,每当用户最小化或离开窗口单击时都需要注销 在我的App.xaml.cs中,我为挂起事件注册了一个事件处理程序。然后,我将注销代码放入此事件处理程序中,如下所示:C# 当处于平板电脑模式且代码处于OnSuspended时,发布模式UWP应用程序崩溃,c#,xamarin,xamarin.forms,uwp,C#,Xamarin,Xamarin.forms,Uwp,我们有一个Xamarin UWP应用程序,每当用户最小化或离开窗口单击时都需要注销 在我的App.xaml.cs中,我为挂起事件注册了一个事件处理程序。然后,我将注销代码放入此事件处理程序中,如下所示: private void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.GetDeferral(); AppBackgrounded(); deferral.Complete()
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.GetDeferral();
AppBackgrounded();
deferral.Complete();
}
此AppBackground()方法如下所示:
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();
}
}
我们还有一个AppLeavingBackground方法,用于在用户返回时还原应用程序,但返回时应用程序不会崩溃。它仅在运行OnSuspended方法时崩溃
只有在以下情况下才会发生此崩溃:
这很难调试,因为它只在发布模式下发生。有什么想法吗?我不能说我已经完全阅读了文档,文档中说在挂起状态下导航页面会导致崩溃,但很明显,这是一个错误的地方
挂起不用于准备再次打开应用程序,此生命周期事件的全部原因是您需要准备应用程序,以防再次打开,这意味着保存一些可能最终丢失的数据。准备再次打开应用程序是在“恢复”过程中完成的。在我的例子中,是Clipboard.Clear()函数导致应用程序崩溃。对于遇到类似问题的人,请检查其他答案,因为它们都提供了很好的观点
另外,我还发现在这些事件中使用异步代码会使我的应用程序崩溃。我不知道为什么,因为我使用了延期,但既然它起作用了,我就不打算再继续了 您可能需要先在这些区域进行日志记录,以便在冻结之前确定其位置。还值得添加一些异常处理和附加日志记录,以检查是否引发了任何异常,但不会终止您的应用程序。发布模式使用.net native,您还可以在调试配置中激活.net native并调试应用程序。在不等待的情况下调用异步方法“PushModalAsync”。在Navigation.PushModalAsync完成执行之前,可能会调用deleral.Complete。这可能不是崩溃问题的根源,但问题在于您的代码。在PushModalAsync(…)和AppBackgrounded()调用之前添加wait;然后创建AppBackground和OnSuspended异步方法。