简体   繁体   中英

How do i pass strings from one page to another in this C# app using a button?

In my MainPage.xaml: area and btu have values inside the button function and I changed them to strings.

private void button1_Click(object sender, RoutedEventArgs e)        
{       
    ...

    area.ToString();

    btu.ToString();

    NavigationService.Navigate(new Uri("/SolutionPage.xaml?msg1=" + area, UriKind.Relative));
     NavigationService.Navigate(new Uri("/SolutionPage.xaml?msg2=" + btu, UriKind.Relative));

}

then, in page1.xaml, i have:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    string msg1 = "";
    if (NavigationContext.QueryString.TryGetValue("msg1", out msg1))
    {
        textBlock1.Text = String.Format("Area:{0}", msg1);
    }
    base.OnNavigatedTo(e);
    string msg2 = "";
    if (NavigationContext.QueryString.TryGetValue("msg2", out msg2))
    {
        textBlock2.Text = String.Format("BTU:{0}", msg2);
    }
}

The problem is that only textBlock1 change (Area). What should i do to pass btu.ToString() to textBlock2.text?

You should make the navigation request once, and append both values to it.

string url = string.Format("/SolutionPage.xaml?msg1={0}&msg2={1}", area.toString(), btu.toString());
NavigationService.Navigate(new Uri(url, UriKind.Relative));

And in your OnNavigatedTo , only call the base version once.

you only get to make one call to Navigate . So you're going to need to pass both of those params to the navigate call, like

new Uri( "/SolutionPAge.xaml?msg1=blah&msg2=blah", UriKind.Relative);

and then parse it apart in one call to OnNavigatedTo

if only one changes, just pass only one of those values in the query string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM