Parameter passing between pages

2

How to pass parameter between pages in Windows Phone 8 .1?

In the previous version it was done like this:

Page 1:

  private void Button_Click(object sender, RoutedEventArgs e)
  {
   string uri = string.Format("/Pagina2.xaml?nomeParametro={0}", txtValor.Text);

   NavigationService.Navigate(new Uri(uri, uriKind.Relative));  
  }

Page 2:

  protected override void OnNavigatedTo(NavigationEventArgs e)
  {
   if (NavigationContext.QueryString["nomeParametro"] != null)
  txtParametro.Text = NavigationContext.QueryString["nomeParametro"];

 base.OnNavigatedTo(e);
}

However, in the current version it is no longer possible to use NavigationContext .

How do I proceed?

    
asked by anonymous 28.11.2014 / 18:09

2 answers

3

In version 8.1 you begin to use navigation from the Frame of the page, and receive the parameter from the object passed to the OnNavigatedTo method:

pag 1:

private void Button_Click(object sender, RoutedEventArgs e)
{
    string parametro = txtValor.Text;
    this.Frame.Navigate(typeof(Pagina2), parametro);
}

page 2:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    txtParametro.Text = (string)e.Parameter;
}
    
28.11.2014 / 18:43
2

To navigate between pages, you should use the Navigate (Type pageType) method. And to include parameters, we use the Navigate (Type pageType, object param) method.

For example, consider a page called BasicPage1. We want to navigate to another page by passing a parameter. To do so:

In the BasicPage1 class:

    private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
    {
        this.Frame.Navigate(typeof(BasicPage2), tb1.Text);
    }

In the BasicPage2 class:

    private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e) {
        string message = e.NavigationParameter as string;
        if (!string.IsNullOrWhiteSpace(name)) {
            tb1.Text = "Hello, " + name;
        }
    }

For a more detailed example, please visit the link

    
13.07.2015 / 18:52