Passing parameter between pages

Asked

Viewed 674 times

2

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

In the previous version it was made like this:

Pag 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));  
  }

Pag 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 the NavigationContext.

How should I proceed?

2 answers

3


In version 8.1 you now use the navigation from the Frame page, and takes 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);
}

pag 2:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    txtParametro.Text = (string)e.Parameter;
}

2

To navigate between pages, we must 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 wish from it 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 Class Basicpage2:

    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 link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.