[英]Windows Phone 7 - passing values between pages
I am trying to send the values between the pages using : 我试图使用以下命令在页面之间发送值:
NavigationService.Navigate(new Uri("/ABC.xaml?name=" + Company + "&city=" + City , UriKind.Relative));
Here the Company and City values are passed to the next page, somehow the Company names like "ABC & Ltd" is not passing properly,it just passes the "ABC" to next page. 这里公司和城市的价值被传递到下一页,不知何故公司名称如“ABC&Ltd”没有正确传递,它只是将“ABC”传递到下一页。 Basically the portion after "&" is dropped.
基本上“&”之后的部分被删除。
Is there an option here to format this ??? 这里有格式吗? Or should i write a logic for this ??
或者我应该为此写一个逻辑?
Help needed ! 需要帮助 !
Thanks 谢谢
If any of your query strings contain characters that are considered invalid in a Uri what you're doing will fail, as you've discovered. 如果您的任何查询字符串包含在Uri中被视为无效的字符,那么您所做的事情将会失败,正如您所发现的那样。 You need to use
Uri.EscapeDataString
to escape any illegal characters first. 您需要先使用
Uri.EscapeDataString
来转义任何非法字符。 Change the code you've posted to the following: 将您发布的代码更改为以下内容:
NavigationService.Navigate( new Uri( String.Format( "/ABC.xaml?name={0}&city={1}",
Uri.EscapeDataString( Company ), Uri.EscapeDataString( City ) ),
UriKind.Relative ) );
The escaped strings are automatically unescaped when you read them using NavigationContext.QueryString
, so there's no need to call Uri.UnescapeDataString
explicitly. 当您使用
NavigationContext.QueryString
读取它们时,转义字符串会自动转义 ,因此无需显式调用Uri.UnescapeDataString
。
The &
character is treated as a special character in query strings as a means of separating values. &
字符在查询字符串中被视为特殊字符,作为分隔值的方法。 It needs to be escaped into %26
. 它需要转义为
%26
。
For more information on how to escape URLs easily using Uri.EscapeUriString
. 有关如何使用
Uri.EscapeUriString
轻松转义URL的更多信息。
For example: 例如:
string Company = "ABC & D";
string City = "Falls Church";
string escaped = Uri.EscapeUriString("/ABC.xaml?name=" + Company + "&city=" + City);
var uri = new Uri(escaped, UriKind.Relative);
You Could also pass parameters to you App.xaml.cs code where you can define global values that you can access throughout your app, 您还可以将参数传递给App.xaml.cs代码,您可以在其中定义可在整个应用中访问的全局值,
http://www.developer.nokia.com/Blogs/Community/2011/08/25/passing-data-between-pages-in-windows-phone-7/ http://www.developer.nokia.com/Blogs/Community/2011/08/25/passing-data-between-pages-in-windows-phone-7/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.