How to pass values (parameters) between XAML pages?

75,337

Methods to pass parameters

1. Using the query string

You can pass parameters through the query string, using this method means have to convert your data to strings and url encode them. You should only use this to pass simple data.

Navigating page:

page.NavigationService.Navigate(new Uri("/Views/Page.xaml?parameter=test", UriKind.Relative));

Destination page:

string parameter = string.Empty;
if (NavigationContext.QueryString.TryGetValue("parameter", out parameter)) {
    this.label.Text = parameter;
}

2. Using NavigationEventArgs

Navigating page:

page.NavigationService.Navigate(new Uri("/Views/Page.xaml?parameter=test", UriKind.Relative));

// and ..

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    // NavigationEventArgs returns destination page
    Page destinationPage = e.Content as Page;
    if (destinationPage != null) {

        // Change property of destination page
        destinationPage.PublicProperty = "String or object..";
    }
}

Destination page:

// Just use the value of "PublicProperty"..

3. Using Manual navigation

Navigating page:

page.NavigationService.Navigate(new Page("passing a string to the constructor"));

Destination page:

public Page(string value) {
    // Use the value in the constructor...
}

Difference between Uri and manual navigation

I think the main difference here is the application life cycle. Pages created manually are kept in memory for navigation reasons. Read more about it here.

Passing complex objects

You can use method one or two to pass complex objects (recommended). You can also add custom properties to the Application class or store data in Application.Current.Properties.

Share:
75,337
Daniel Little
Author by

Daniel Little

I'm a Software Engineer based in Australia. I enjoy building Team Culture, Type Safety, Functional Programming, Event Sourcing, Event Driven Systems and Distributed Systems. People first. I write occasionally at daniellittle.dev and on twitter as @daniellittledev.

Updated on January 12, 2022

Comments

  • Daniel Little
    Daniel Little over 2 years

    Similar questions have been asked before but this question strives to explore more options and the ability to pass complex objects.

    The question is how to pass parameters but it really needs to be broken up into three parts..

    1. When navigating between pages in an XAML application how do you pass parameters?
    2. What is the difference between using the Uri navigation and manual navigation?
    3. How can objects (not just strings) be passed when using Uri navigation?

    Example of Uri navigation

    page.NavigationService.Navigate(new Uri("/Views/Page.xaml", UriKind.Relative));
    

    Example of manual navigation

    page.NavigationService.Navigate(new Page());
    

    The answer to this question applies to WP7, silverlight, WPF and Windows 8.

    Note: There is a difference between Silverlight and Windows8

    • Windows Phone: pages are navigated to using a Uri and data passed as a query string or an instance
    • Windows 8: pages are navigated to by passing the type, and parameters as objects
  • Vishal
    Vishal over 10 years
    It may be additionally noted that NavigationContext.QueryString.TryGetValue("parameter", out parameter) needs to be called from inside the following method: protected override void OnNavigatedTo(NavigationEventArgs e)
  • fnc12
    fnc12 about 4 years
    what about navigation with typeof operator? You wrote nothing about it