Click here to Skip to main content
11,565,352 members (38,986 online)
Click here to Skip to main content

MVVM Light Toolkit and Windows Phone : Navigation Between Pages

, 13 Aug 2014 CPOL 5.4K 230 9
Rate this:
Please Sign up or sign in to vote.
This tip/trick shows you how to add navigation between pages with MVVM Light Toolkit in Windows Phone 8

Introduction

The following code snippets will show you how to add the basic navigation between pages in Windows phone 8 with MVVM Light Toolkit.

Using the code

To achieve our goal, as mentioned before, we will need to implement an Interface and a Class.

"INavigationService" interface :

First, we'll begin by adding "INavigationService" interface :

public interface INavigationService
{
    event NavigatingCancelEventHandler Navigating;
    void NavigateTo(Uri pageUri);
    void GoBack();
}

It allows :

  • Navigation to a given URI.
  • Going back
  • Being notified when a navigation is taking place, and be able to cancel

"NavigationService" class :

Then, we'll add the "NavigationService" class

public class NavigationService : INavigationService
{
    private PhoneApplicationFrame _mainFrame;

    public event NavigatingCancelEventHandler Navigating;

    public void NavigateTo(Uri pageUri)
    {
        if (EnsureMainFrame())
        {
            _mainFrame.Navigate(pageUri);
        }
    }

    public void GoBack()
    {
        if (EnsureMainFrame() && _mainFrame.CanGoBack)
        {
            _mainFrame.GoBack();
        }
    }

    private bool EnsureMainFrame()
    {
        if (_mainFrame != null)
        {
            return true;
        }

        _mainFrame = Application.Current.RootVisual as PhoneApplicationFrame;

        if (_mainFrame != null)
        {
            // Could be null if the app runs inside a design tool
            _mainFrame.Navigating += (s, e) =>
            {
                if (Navigating != null)
                {
                    Navigating(s, e);
                }
            };

            return true;
        }

        return false;
    }
}

Registering the Navigation Service (ViewModelLocator):

Before using the Navigation Service, we need to register it in the IOC (I'm using SimpleIoc).

SimpleIoc.Default.Register<INavigationService, NavigationService>();

Using the Navigation Service (MainPage):

navigationService = SimpleIoc.Default.GetInstance<INavigationService>();

Navigating to the next page (MainPage):

As the classic way to navigate to other pages, using this method is quite simple. Just use it as you do in a simple app.

You can also add parameters.

navigationService.NavigateTo(new Uri("/SecondPage.xaml?ParamName=ParamValue", UriKind.Relative));

 

That's it! I hope it was helpful.

History

This is the first version of this tip/trick. If you have any problem/question, feel free to comment.

You're welcome. Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mdimagh Firas
Student ISAMM
Tunisia Tunisia
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 14 Aug 2014
Article Copyright 2014 by Mdimagh Firas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid