Search code examples
c#windows-phone-8windows-phone-8.1navigationservice

Passing class data on navigation to other page Windows Phone 8.1


I've a class

class PTD
{
  public string Player1 { get; set; }
  public string Player2 { get; set; }
}

And a Page1 have 2 text boxes and 1 button. The Button Click Method have following code:

PTD ptd = new PTD();
ptd.Player1=textbox1.text.ToString();
ptd.Player2=textbox2.text.ToString();

NavigationService.Navigate(new Uri ("/Page2.xaml?msg=", UriKind.RelativeOrAbsolute));

On Page2 I have 2 text blocks where i want my class data to appear on navigation. What additional code should i write to perform this action?


Solution

  • In Windows Phone 8.0 and Windows Phone Silverlight 8.1, you can use NavigationService to do page navigations.

    You can pass strings as parameter of 'Navigation Uri'.

    NavigationService.Navigate(new Uri (
        string.Format("/Page2.xaml?player1={0}&player2={1}", ptd.Player1, ptd.Player2), 
        UriKind.RelativeOrAbsolute));
    

    Then in Page2, you can receive the 2 params in OnNavigatedTo method like this:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        string player1;
        NavigationContext.QueryString.TryGetValue("player1", out player1);
        string player2;
        NavigationContext.QueryString.TryGetValue("player2", out player2);
    }