应用程序在暂停时崩溃 [英] App crashes when on Suspend

查看:39
本文介绍了应用程序在暂停时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了一些奇怪的行为.

So I ran across some bizarre behavior.

我是第一个使用 ViewModel 的页面.ViewModel 只包含一个 ObservableCollection.当我浏览第二页时,一切似乎都很好.但是当我在这个页面上暂停应用程序时,应用程序崩溃了.长按后退按钮或 Windows 键.奇怪的是,这只发生在我不调试的时候.当我调试一切正常发现.暂停 - 恢复工作完美.

I am an the first page with a ViewModel. The ViewModel only contains a ObservableCollection. When I navigate on the second page everything seems fine. But when I suspend the app on this page, the app crashes. Long-Press Back-Button or Windows Key. The bizarre thing is, that this only happens when I am not debugging. When I am debugging everything works find. Suspend-Resume works perfectly.

我可以在我的 Lumia 920 和模拟器上重现它.

I could reproduce it on my Lumia 920 and the Emulator.

我有点找到了造成这种行为的原因,但实际上应该是一个原因.

I kinda found the cause which is responsible for this behavior, but it should be a cause actually.

当我离开时,我正在调用这个方法

When I navigate away, I am calling this method

Frame.Navigate(typeof(SecondPage), FirstPageViewModel);  //names are fictive

奇怪行为的原因是 ObservableCollection.如果我只是传递一个字符串对象就可以了.如果我创建一个本地列表并传递它,它会在挂起时崩溃.

The cause of the weird behavior is the ObservableCollection. If I just pass a string object it is fine. If I create a local List and pass it, it crashes on Suspending.

我在想,我无法传递 List 对象,但这没有任何意义.为什么它不会发生在调试器中.有没有可能我在某处用完了内存?

I am thinking, I am unable to pass a List object, which does not make any sense though. Why does it not happen in Debugger. Is it possible I run out of memory somehow somewhere?

推荐答案

该问题与调试模式无关,因为在调试时,未引发 Suspending 事件,因此您的应用运行良好.

The issue doesn't concern debug mode, because while debugging, the Suspending event is not raised, so your app works fine.

为了更好地了解正在发生的事情,请手动调用挂起事件(请参阅上面链接中的答案).然后您将能够在调试模式下看到抛出异常(及其种类)的情况.

To better see what is happening, invoke the suspending event manually (see answer at the link above). Then you will be able to see in debug mode, where the exception (and its kind) is thrown.

我无法说出问题的确切原因(没有看到代码),但是您提供了一行可能会很麻烦的代码:

I cannot say what exacly causes the problem (without seeing the code), but you have provided one line which can be troublesome:

Frame.Navigate(typeof(SecondPage), FirstPageViewModel);

您在此处导航并传递一个对象,因此您可能在某处检索它.看看当您恢复应用程序时,页面已恢复,但您的对象通常不会.您将不得不使用一些 NavigationHelper 的 方法 - SaveState/LoadState 来保存/加载您传递的数据或自己处理.

You navigate here and pass an object, so probably somewhere you retrive it. Look that when you are resuming your app, the page is restored, but your object normally is not. You will have to use some NavigationHelper's methods - SaveState/LoadState to save/load your passed data or handle this yourself.

这篇关于应用程序在暂停时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆