簡體   English   中英

如何使用按鈕在此C#應用程序中將字符串從一頁傳遞到另一頁?

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

在我的MainPage.xaml中:area和btu在按鈕函數中具有值,我將其更改為字符串。

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

}

然后,在page1.xaml中,我有:

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

問題是只有textBlock1更改(區域)。 我應該怎么做才能將btu.ToString()傳遞給textBlock2.text?

您應該發出一次導航請求,並將兩個值都附加到它。

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

並且在您的OnNavigatedTo ,僅調用一次基本版本。

您只能打一個電話給Navigate 因此,您需要將這兩個參數都傳遞給導航調用,例如

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

然后在一次對OnNavigatedTo調用OnNavigatedTo其解析

如果只有一個更改,則只需在查詢字符串中傳遞這些值之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM