Home > C#, MVVM, Windows Phone, WinRT > An improved NavigationService for MVVM Light in Universal apps

An improved NavigationService for MVVM Light in Universal apps

24/02/2015

Some times ago we talked about the new NavigationService that has been introduced with MVVM Light 5.

The implementation for Universal apps is very useful as it covers all the basic needs. However, at this time it lacks some important features, like a way to determine whether the navigation can go back and a method to remove the most recent available entry from the back stack.

So, we can extend the built-in NavigationService defining an interface like this:

public interface INavigationServiceEx : INavigationService
{
    bool CanGoBack { get; }
    
    bool RemoveBackEntry();       
}

And the corresponding implementation:

public class NavigationServiceEx : NavigationService, INavigationServiceEx
{
    public bool CanGoBack
    {
        get
        {
            var frame = this.GetMainFrame();
            if (frame != null)
                return frame.CanGoBack;

            return false;
        }
    }

    public bool RemoveBackEntry()
    {
        var frame = this.GetMainFrame();
        if (frame.CanGoBack)
        {
            frame.BackStack.RemoveAt(frame.BackStackDepth - 1);
            return true;
        }

        return false;
    }

    private Frame GetMainFrame()
    {
        return (Frame)Window.Current.Content;
    }
}

In this class, we simply get the current Frame and then access its CanGoBack and BackStack property.

Now we can use the new NavigationServiceEx in the usual way:

static ViewModelLocator()
{
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

    var navigationService = CreateNavigationService();
    SimpleIoc.Default.Register<INavigationServiceEx>(() => navigationService);

    // ...
}

private static INavigationServiceEx CreateNavigationService()
{
    var navigationService = new NavigationServiceEx();
    navigationService.Configure(Constants.SecondPage, typeof(SecondPage));

    return navigationService;
}

Of course, all ViewModels need to refer the new INavigationServiceEx interface (instead of the default INavigationService provided by MVVM Light).

Categories: C#, MVVM, Windows Phone, WinRT
  1. 12/05/2015 at 02:26

    Hi, thanks for you article, and I meet another question, if you navigate much more pages, your memory will raise a lot, and the workaround is just setting the navigation cache to Enable. And I don’t know why the default navigation cache setting of the page is Disable? Appreciate for your reply, many thanks.

    • 12/05/2015 at 08:27

      Because NavigationCacheMode=”Enabled” has some drawbacks. For example, with this settings, all pages are cached until the App is terminated (or until the cache is full). What happen if we want to remove a page from the cache when calling GoBack() to close it? Take a loook to this post for more information: http://www.mobilemotion.eu/?p=955.

  1. 24/02/2015 at 14:25
Comments are closed.
%d bloggers like this: