Pass more than one parameter to another page

Asked

Viewed 901 times

2

I’m making a windows phone application, in which the main screen( first screen) is made a calculation, and the result is displayed on a second screen.

I already managed to make the 'connection' between the two screens as follows

On page 1 or main screen:

     private void button2_Click(object sender, RoutedEventArgs e)
    {

        Uri caminho = new Uri("/Paginas/Resolucao.xaml?parametro=" textBlock1.Text, UriKind.Relative);
        NavigationService.Navigate(caminho);

    }

On Page 2 or secondary screen

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        string parametroRecebido1 = NavigationContext.QueryString["parametro"];
        textBlock2.Text = parametroRecebido1;       
    }

But with these 'methods' I can only pass a variable( the variable that is in the textBlock1) I saw on that site http://www.geekchamp.com/tips/how-to-pass-data-between-pages-in-windows-phone-alternatives on Option3 that in order to pass more than one variable the following code would have to be done:

On page 1 or main screen;

    private void btnNavigate_Click(object sender, RoutedEventArgs e)
    {
    NavigationService.Navigate(new Uri("/Page1.xaml?parameter1=p1& parameter2=p2",UriKind.Relative));
    }

On the Second Screen;

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
     base.OnNavigatedTo(e);
     string parameterValue1 = NavigationContext.QueryString["parameter1"];
     string parameterValue2 = NavigationContext.QueryString["parameter2"];  
    }

____ My question is this: : how do I pass for example my variables n1 and N2 in the first screen method

     NavigationService.Navigate(new Uri("/Page1.xaml?parameter1=p1&parameter2=p2", UriKind.Relative));

___ I tried to fit my variables n1 and N2 into this line of code as follows: ---- But GAVE ERROR

    NavigationService.Navigate(new Uri("/Page1.xaml?parameter1="+n1&+"parameter2=n2", UriKind.Relative));

___ I also tried to keep the code 'original' by modifying the name of the variables Thus:

     NavigationService.Navigate(new Uri("/Page1.xaml?parameter1=n1&parameter2=n2", UriKind.Relative));

But when displaying the results it only displayed the words 'n1' and 'N2' and not the values stored there

Who can help I will be immensely grateful :D

  • Diego, you have missed only "" in the command: Navigationservice.Navigate(new Uri("/Page1.xaml?parameter1=" + n1 + "&parameter2=" + N2, Urikind.Relative)); - Prefer string. Format to avoid these problems.

  • Malkaviano, it worked here, I do not believe I broke my head for hours because of a " but now I know. Thank you aee

  • Good night friend does not post the answer within the question, we are not a simple forum, we are a Q&A, so you can answer your own question by clicking the answer button, plus it is not necessary to edit the title and write SOLVED, by marking the correct answer the question is already considered as solved. A good night

  • Friend I think you are confused, I said in the previous comment that writing SOLVED in the title is not necessary.

  • @Guilhermenascimento I was a little confused even hahaha, but I think it’s all right, sorry, it’s just that I started a few days ago at the forum

  • Precisely we are not forum, we are a community of questions and answers, here the scheme is more organized :) at first it seems boring is difficult, but the organization this through the tool itself make stackexchange communities much easier to moderate and organize.

Show 1 more comment

2 answers

1


You can use Isolatedstoragesettings to store variables before going to the next screen.

To store:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["parameter1"] = this.txtboxDescription;
settings["parameter2"] = this.txtboxDescription2;
settings.Save();

To get, after you have passed to the next page (Navigationservice.Navigate(new Uri("/Page1.xaml')):

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
string txtboxDescription2 = settings["parameter1"];
string txtboxDescription2 = settings["parameter2"];

If it’s not string, you can cast the value:

Boolean saveLogin = (bool)settings["saveLogin"];

1

SOLUTION

I was informed by the user /users/21504/malkaviano that my mistake had only been in the use of quotation marks("). With the error corrected the code is as follows:

Main screen or front page

private void button2_Click(object sender, RoutedEventArgs e)
{
Uri caminho = new Uri("/Paginas/Resolucao.xaml?parametro="+n1+"&parametro2="+n2, UriKind.Relative);
NavigationService.Navigate(caminho);

}

My mistake was to separate & string and put it out of quotes(").

In case n1 and N2 are my variables;

On the Second Screen will look like this:

 protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    string parametroRecebido1 = NavigationContext.QueryString["parametro"]; 
    string parametroRecebido2 = NavigationContext.QueryString["parametro2"];


   textBlock1.Text = parametroRecebido1;
   textBlock2.Text = parametroRecebido2;

}

If you also want to pass from 1 to as many variables Voce want Voce will do as follows: In this case I will exemplify with three variables(n1,N2,N3)

On Main Screen

private void button2_Click(object sender, RoutedEventArgs e)
{
Uri caminho = new Uri("/Paginas/Resolucao.xaml?parametro="+n1+"&parametro2="+n2+"&parametro3="+n3 UriKind.Relative);
NavigationService.Navigate(caminho);

}

On the second screen

 protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    string parametroRecebido1 = NavigationContext.QueryString["parametro"]; 
    string parametroRecebido2 = NavigationContext.QueryString["parametro2"];
    string parametroRecebido3 = NavigationContext.QueryString["parametro3"];

   textBlock1.Text = parametroRecebido1;
   textBlock2.Text = parametroRecebido2;
   textBlock3.Text = parametroRecebido3;

}

Browser other questions tagged

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