昨天在開發windows phone 8.1程序時,發現程序在掛起的時候,會自動退出。經過調試發現錯誤信息是windows
System.Runtime.InteropServices.COMException (0x80004005): Unspecified error GetNavigationState doesn't support serialization of a parameter type which was passed to Frame.Navigate. at Windows.UI.Xaml.Controls.Frame.GetNavigationState() at TX.WifiSurfing.Common.SuspensionManager.SaveFrameNavigationState(Frame frame) at TX.WifiSurfing.Common.SuspensionManager.<SaveAsync>d__0.MoveNext() System.Collections.ListDictionaryInternal
意思是因爲頁面導航的參數不支持序列化,程序掛起時保存狀態的時候出現了異常:app
/// <summary> /// 在將要掛起應用程序執行時調用。 將保存應用程序狀態 /// 將被終止仍是恢復的狀況下保存應用程序狀態, /// 並讓內存內容保持不變。 /// </summary> private async void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); await SuspensionManager.SaveAsync(); deferral.Complete(); }
可是我整個程序並無在導航的時候傳遞對象,出現這問題很讓我費解。經過查看異常頁面的OnNavigatedTo方法,發現當我使用NavigateToPageAction導航的時候,若是Parameter不設置,程序會設置一個默認值,因爲我是使用EventTriggerBehavior的Tapped事件觸發的,Parameter被設置成了TappedRoutedEventArgs,這是一個不可序列化的對象,因此在掛起時保存狀態的時候出現了錯誤。把它設置成Parameter=「 」是一個解決方案(注:設置成Parameter=「」和Parameter=「{x:null}」都不能夠,仍是原來的默認值)。async