Friday, September 18, 2015

MVVMCross navigation issues with Windows Phone 8.1 Silverlight - RootFrame_NavigationFailed

Problem:

Had a working Windows Phone 8 project as part of our cross platform solution.  I upgraded the Windows Phone UI to WP Silverlight 8.1.  I immediately began getting strange behaviour on reactivation.

The issue appeared like so.  I can successfully launch the app and it works with no issues.  I then hit the windows button to take me out of the application.  Upon touching the tile to reactivate, the application loads and then crashes.  This does not occur in the previous Windows Phone 8 version.

Issue:

The app activation and deactivation behaves differently in Windows 8.1.  We are using MVVMCross to accomplish our IoC.  Thus for Windows Phone 8, the app.xaml.cs includes an override method in the Application_Launching event which instantiates the IoC to pass control back to the Core for resolution.  This works great in WP8 but due to the changes in navigation in WP8.1, the IoC event does not get called again when the app reloads from memory.  The next issue is that the WP8.1 navigation load overwrites the previous MVVM view in the RootFrame and replaces it with the xaml file specified in the WMAppManifest.xaml file.  This naturally will cause either a crash if you have deleted the MainPage.xaml from your project like I did, or it will load that MainPage.xaml instead of the correct MVVM view.

Solution:


Need to intercept the navigation stack and manually remove the offending page from the stack.
This can be done by going to the App.xaml.cs page in your Windows Phone project.

private void Application_Launching(object sender, LaunchingEventArgs e)
{
      RootFrame.Navigating += RootFrameOnNavigating;
    RootFrame.Navigating += (s, e) =>
    {
    if (e.Uri.ToString().StartsWith("/MainPage.xaml"))
    {
        e.Cancel = true;
    }
    };
}

Source
  1. https://msdn.microsoft.com/en-us/library/windows/apps/ff817008%28v=vs.105%29.aspx
  2. http://stackoverflow.com/questions/23324213/mvvmcross-phone-8-1-resume-shows-mainpage-xaml
  3. http://fetchmytip.blogspot.com/2015/08/mvvmcross-systemreflectiontargetinvocat.html

No comments:

Post a Comment