App crashes when on Suspend

2019-07-18 03:23发布

So I ran across some bizarre behavior.

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.

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

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.

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?

2条回答
老娘就宠你
2楼-- · 2019-07-18 03:55

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);

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.

查看更多
做自己的国王
3楼-- · 2019-07-18 03:57

If you're using the builtin SuspensionManager class, you can only pass "simple" types (string, char, numeric, GUID) as the second argument to Frame.Navigate(). See the documentation for Frame.Navigate for more info.

查看更多
登录 后发表回答