Windows Phone 8.1 and Windows Runtime Apps How to #5 – How to Pass Objects when Navigating between pages in Windows Phone 8.1 XAML App ?

In Windows Phone 8.0 and earlier version , the developers could use the NavigationService.Navigate method to navigate from one page to another .

For example , below code snippet could be used to navigate to BasicPage1.xaml from Page1 .

NavigationService.Navigate(new Uri("/BasicPage1.xaml", UriKind.Relative));

If you need to pass a parameter , we could do that by passing them along with the URL. If you targeting Windows Phone 8.1 Silverlight App , this technique will still work fine . How can this be done in Windows Runtime App ? . How to perform navigation and pass complex objects between pages in Windows Phone 8.1 Windows Runtime App/XAML App ?

How to Pass Objects when Navigating between pages in Windows Phone 8.1 XAML App ?

The developers can use the Frame.Navigate method to navigate from one page to another . To navigate to BasicPage1 , you can use the method Frame.Navigate as shown below.

Frame.Navigate(typeof(BasicPage1));

The next step would be to pass data to the second page . Assume that we have an Employee Object which needs to be passed during navigation  .

public class Employee
{
        public string Name { get; set; }
        public int ID { get; set; }
}

 

The Frame.Navigate method takes 2nd parameter of type object which can be used to pass parameters.

 Employee emp = new Employee { ID = 1, Name = "Senthil" };
 Frame.Navigate(typeof(BasicPage1), emp); 

Note : The Basic Page template is used when adding/creating the page BasicPage1 , this will add the necessary event handlers to receive the passed parameter using NavigationHelper_LoadState method . This support is available is the page templates like  Basic Page , Group Detail Page , Grouped Items Page , Item Detail Page , Items Page , Split Page , Hub Page , Search Results Page etc.

The final step would be to retrieve the data from the passed parameter in the NavigationHelper_LoadState event handler and use them in the screen .

private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
    Employee emp = e.NavigationParameter as Employee;
    if(emp!=null)
    {
       txtName.Text = emp.Name;
       txtID.Text = emp.ID.ToString();
    }
}
1
How to Pass Objects when Navigating between pages in Windows Phone 8.1 XAML App ?

Update : Thanks to Arunjeet singh for bring this topic . When passing the complex objects via the Navigate method , you might have an issue when using the Frame.GetNavigationState method. This might throw an exception . The GetNavigationState supports serialization only for basic types like string, char, numeric and GUID types.

How to solve this issue ? . In one of the episode of Defrag tools ,  Andrew Richards and Chad Beeder walk you through a common issue in Windows Store applications that use Frame.GetNavigationState and talk about the possible solution as well . Below is the URL for watching the video.

http://channel9.msdn.com/Shows/Defrag-Tools/Defrag-Tools-74-Windows-81-FrameGetNavigationState-Crash#time=07m00s

    1 Comment

  1. shivam
    August 23, 2014
    Reply

    frame.navigate() method is for wp 8.1 runtime apps only
    in silverlight apps we need to use navigate.navigationservice() method

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This blog post will guide you through several effective methods to troubleshoot and resolve the issue of Microsoft Edge not...
Windows 11 offers a range of audio enhancements that can enrich your listening experience. These enhancements include features like virtual...
Windows 11 brings a fresh and visually stunning design to your desktop, and one of the standout features is the...