【问题标题】:How to pass multiple string values from one page to another?如何将多个字符串值从一页传递到另一页?
【发布时间】:2014-10-23 20:40:06
【问题描述】:

我想出了如何使用查询字符串将一个值作为字符串从一个页面从一个页面传递到另一个页面。

但我不知道如何传递多个字符串值,例如我有一个 wrkTbx、rstTbx 和 roundTbx。

如何将所有三个值传递到另一个页面?

这是我当前的实现:

锻炼页面:

        private void appBarAddBtn_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml?key=" + wrkTbx.Text, UriKind.Relative));
        }

主页:

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (NavigationContext.QueryString.ContainsKey("key"))
            {
                string val = NavigationContext.QueryString["key"];
                MessageBox.Show("value is:  " + val);
            }

        }

【问题讨论】:

标签: c# navigation query-string windows-phone-8.1


【解决方案1】:

您可以使用与号 (&) 分隔多个 QueryString 参数。但您还需要确保正确编码这些值,因为它们可能包含特殊字符(例如 & 符号)。

这是您的代码的修改版本,它传递了您提到的所有值。

锻炼页面:

private void appBarAddBtn_Click(object sender, EventArgs e)
{
    string url = "/MainPage.xaml" +
        "?wrkTbx=" + System.Net.WebUtility.UrlEncode(wrkTbx.Text) +
        "&rstTbx=" + System.Net.WebUtility.UrlEncode(rstTbx.Text) +
        "&roundTbx=" + System.Net.WebUtility.UrlEncode(roundTbx.Text);

    NavigationService.Navigate(new Uri(url, UriKind.Relative));
}

主页:

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

    if (NavigationContext.QueryString.ContainsKey("wrkTbx"))
    {
        string wrkTbx = NavigationContext.QueryString["wrkTbx"];
        MessageBox.Show("wrkTbx value is: " + wrkTbx);
    }

    if (NavigationContext.QueryString.ContainsKey("rstTbx"))
    {
        string rstTbx = NavigationContext.QueryString["rstTbx"];
        MessageBox.Show("rstTbx value is: " + rstTbx);
    }

    if (NavigationContext.QueryString.ContainsKey("roundTbx"))
    {
        string roundTbx = NavigationContext.QueryString["roundTbx"];
        MessageBox.Show("roundTbx value is: " + roundTbx);
    }

}

【讨论】:

  • 名称“服务器”不存在。这在 windows phone 8.1 中可用吗?我必须添加一些描述的使用吗?
  • 我在发布该代码后意识到它使用的是 ASP.NET 版本的 UrlEncode。检查修改后的“锻炼页面”代码,以及我在上面添加的“注意”,看看是否有效。
  • 刚刚尝试编辑的代码,System.web 无法识别。添加了一个 using.System。但没有 .Web 的参考
  • 您是否添加了对System.Web.dll 程序集的项目引用? (假设它甚至可以在 Windows Phone 应用程序受限的 .NET 框架的“便携式”版本中使用......)
  • 我不认为它可用,尝试在解决方案资源管理器中添加引用,但没有 System.Web
【解决方案2】:
Response.Redirect(String.Format("Default2.aspx?wrkTbx.Text={0}&rstTbx.Text={1}&roundTbx.Text={2}",Server.UrlEncode(wrkTbx.Text),Server.UrlEncode(rstTbx.Text),Server.UrlEncode(roundTbx.Text)));


Server.UrlDecode(Request.QueryString["wrkTbx.Text"]);
Server.UrlDecode(Request.QueryString["rstTbx.Text"]);
Server.UrlDecode(Request.QueryString["roundTbx.Text"]);

【讨论】:

    【解决方案3】:

    对于 wp8

    //send data
    NavigationService.Navigate(new Uri("/MainPage.xaml?key=" + wrkTbx.Text + "&key2=" + wrkTbx2.Text, UriKind.Relative));
    
    //retrive the information 
    string key,stringKey2;
    int key2;
    if (NavigationContext.QueryString.TryGetValue("key", out key)){
      // use key value
    }
    if (NavigationContext.QueryString.TryGetValue("key2", out stringKey2)){
        key2 = Int32.Parse(stringKey2); // use key2 value
    }
    

    对于windows phone8.1

    public class model
    {
       public string key{ get; set; }
       public int key2{ get; set; }
    }
    
    //send data
    Frame.Navigate(typeof(MainPage), new PassedData { key= "my name", key2= 10 });
    
    // get data
    protected override void OnNavigatedTo(NavigationEventArgs e){
        model= e.Parameter as model;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多