Today I’ve found an issue with Fast App Switching in Windows Phone 8 while using MvvmCross.

The main problem is that MvvmCross navigation model will not be enough to make it work. When resuming the app, MvvmCross will correctly load the view and the viewmodel. However, the system will launch a navigating event that won’t be handled by MvvmCross and you will end up with a navigation error or a view loaded without datacontext, depending on your app’s configuration, as the navigation event that the system will launch will be directing your navigation to whatever page you have set in your project’s WMAppManifest.xml file.

If the path does not fall into your Views folder you have a problem, as an InvalidOperationException will be thrown (“No XAML was found at the location …”). On the other hand, if you have the view in your WMAppManifest.xml file correctly set this will create a new view without any datacontext assigned as this navigation has not been executed by the MvvmCross framework and, therefore, the viewmodel creation and binding won’t be done at all.

For a deeper understanding on what FAS (Fast App Switching or Fast App Resume) is please take a look at this link.

My solution to this problem is posted below. I’ve just managed the activated and navigating events in order to find out whether the app is resuming in FAS mode or not. If that’s the case I cancel the second navigation event launched by the system (emulating what MvvmCross does when the app starts fresh) and voilà, it seems to be working fine.

For me it’s working. Maybe you will have to tune the code to suit your particular scenario.

In order to make it work just paste this code in your App constructor:

FASSupport.Initialize(); 

And add this class to your phone application project:


public class FASSupport
{
    private bool _isPreserved;
    private bool _isReset;
    private static FASSupport _instance ;

    private FASSupport()
    {
        //http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj735579(v=vs.105).aspx
        App.RootFrame.Navigating += RootFrame_Navigating;
        App.RootFrame.Navigated += RootFrame_Navigated;
        PhoneApplicationService.Current.Activated += Current_Activated;
    }

    public static void Initialize()
    {
        if(_instance==null)
            _instance= new FASSupport();
    }

    public void Reset()
    {
        _isPreserved = false;
        _isReset = false;
    }

    void Current_Activated(object sender, ActivatedEventArgs e)
    {
        _isPreserved = e.IsApplicationInstancePreserved;
    }

    void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        if (_isPreserved && _isReset)
        {
            e.Cancel = true;
            Reset();
        }
    }

    void RootFrame_Navigated(object sender, NavigationEventArgs e)
    {
        _isReset = e.NavigationMode == NavigationMode.Reset;
    }

}

Let me know if you have any comments or thoughts. 😀