Why does Frame.GetNavigationState() throw "GetNavigationState doesn't support serialization of a parameter type which was passed to Frame.Navigate" occasionally on suspending?

Hong 1,046 Reputation points
2021-03-24T23:10:45.093+00:00
        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
...
           var deferral = e.SuspendingOperation.GetDeferral();
           string sState = (Window.Current.Content as Frame).GetNavigationState();
...
        }

The above code works fine most of the time, but throws the following exception occasionally:

System.Exception: Unspecified error

GetNavigationState doesn't support serialization of a parameter type which was passed to Frame.Navigate.
Stack Trace:
at System.Runtime.InteropServices.McgMarshal.ThrowOnExternalCallFailed(Int32, RuntimeTypeHandle) + 0x21
at __Interop.ComCallHelpers.Call(__ComObject, RuntimeTypeHandle, Int32, Void*) + 0xbe
at __Interop.ForwardComStubs.Stub_2TThis + 0x36
at Windows.UI.Xaml.Controls.Frame.GetNavigationState() + 0x18

I am wondering if anyone could shed some light on the possible causes of this. I am unable to reproduce this, but an app's analytics reports quite a few of this exception.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. AryaDing-MSFT 2,841 Reputation points
    2021-03-25T06:14:13.4+00:00

    Hi,

    Welcome to Microsoft Q&A!

    Derive from the official document, to enable frame state serialization using GetNavigationState, you must use only basic types for the navigation parameter, such as string, char, numeric, and GUID types. Otherwise GetNavigationState will throw an exception when the app suspends. The parameter can have other types if you do not use GetNavigationState.

    In a word, if you use GetNavigationState method, you can’t pass complex objects using Frame.Navigate. As follows:

    this.Frame. Navigate(Type, Object); //this object can’t be a complex object.  
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful