App crash on startup caused by nonexistent "LastNavigationParameter" key in the session state dictionary in FrameNavigationService.RestoreSavedNavigation()


This randomly started happening when I would launch my app from the Windows Phone emulator (not debugging in Visual Studio). The app would open then immediately close, indicating an unhandled exception.

I managed to trace it down to this method in FrameNavigationService.cs:
public void RestoreSavedNavigation()
    var navigationParameter = _sessionStateService.SessionState[LastNavigationParameterKey];
    NavigateToCurrentViewModel(NavigationMode.Refresh, navigationParameter);
The SessionState dictionary contained only one item: <"AppFrame", 1>, and so accessing "LastNavigationParameter" raised a System.Collections.Generic.KeyNotFoundException.

The caller of the RestoreSavedNavigation() method is MvvmAppBase.InitializeFrameAsync() inside the condition where the previous execution state was Terminated, and it does a try-catch where it will catch only SessionStateServiceException and happily continue. Would it be possible to either:
  • Catch also KeyNotFoundException, or
  • Check if the "LastNavigationParameter" key is nonexistent and then throw SessionStateServiceException


c0sm0 wrote Jun 4, 2015 at 4:38 AM

Is anyone aware of an easy workaround for this issue?