页面之间传递一个字符串中的Windows Phone 8 [英] Passing a string between pages in Windows Phone 8

查看:111
本文介绍了页面之间传递一个字符串中的Windows Phone 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过在Windows Phone的两页8.我一直在寻找周围的一个简单的字符串,试图找到这样做的最好方法 - 但我试过的那些被证明行不通,因为他们应该 - 所以我问你:什么是传中的Windows Phone 8两个页面之间简单的字符串这是我使用导航到其他页面的方法的最佳方式:

  NavigationService.Navigate(新的URI(/ newpage.xaml,Urikind.Relative)); 


解决方案

对于一个字符串变量,它是最容易使用的查询字符串参数:

  NavigationService.Navigate(新的URI(?/ newpage.xaml键=值,Urikind.Relative)) ; 



把它捡起来使用的 NavigationContext.QueryString

 保护覆盖无效的OnNavigatedTo(NavigationEventArgs E)
{
如果(NavigationContext.QueryString .ContainsKey(键))
{
串VAL = NavigationContext.QueryString [钥匙];
//等等......
}
}






注意:如果你的字符串只包含字母数字字符,那么上面的将工作无需修改。但是,如果你的字符串可能有网址保留字符(例如,&放大器; ),那么你会有URL编码他们。使用helper方法 Uri.EscapeDataString 和的 Uri.UnescapeDataString 这一点。



要逃避:

 字符串encodedValue = Uri.EscapeDataString( R&安培; R); 
NavigationService.Navigate(新的URI(/ newpage.xaml键=?+ encodedValue,Urikind.Relative));

要UNESCAPE:

 字符串encodedValue = NavigationContext.QueryString [钥匙]; 
串VAL = Uri.UnescapeDataString(encodedValue);


I need to pass a simple string between two pages in Windows Phone 8. I've been searching around, trying to find the best way of doing it - but the ones i tried turned out to not work as they should - so i ask you: What is the best way to pass a simple string between two pages in Windows Phone 8. This is the method I use to navigate to the other page:

NavigationService.Navigate(new Uri("/newpage.xaml", Urikind.Relative));

解决方案

For a string variable, it's easiest to use a query string parameter:

NavigationService.Navigate(new Uri("/newpage.xaml?key=value", Urikind.Relative));

Pick it up on the target page using NavigationContext.QueryString:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (NavigationContext.QueryString.ContainsKey("key"))
    {
         string val = NavigationContext.QueryString["key"];
         // etc ...
    }
}


Note: if your string contains only alphanumeric characters, then the above will work without modification. But, if your string might have URL-reserved characters (eg, &, ?), then you'll have to URL-encode them. Use the helper methods Uri.EscapeDataString and Uri.UnescapeDataString for this.

To escape:

string encodedValue = Uri.EscapeDataString("R&R");
NavigationService.Navigate(new Uri("/newpage.xaml?key=" + encodedValue, Urikind.Relative));

To unescape:

string encodedValue = NavigationContext.QueryString["key"];
string val = Uri.UnescapeDataString(encodedValue);

这篇关于页面之间传递一个字符串中的Windows Phone 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆