有查询字符串方法,但实现起来很麻烦.
导航时,像HTTP查询字符串一样传递参数.
然后,在其他方面,检查密钥是否存在,并提取值.这样做的缺点是如果你需要做超过1,你需要自己键入它,它只支持字符串.
所以要传递一个整数,你需要转换它.(要传递一个复杂的对象,你需要把你需要的所有部分重新编译到另一边)
NavigationService.Navigate(new Uri("/PanoramaPage1.xaml?selected=item2", UriKind.Relative));
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string selected = String.Empty;
//check to see if the selected parameter was passed.
if (NavigationContext.QueryString.ContainsKey("selected"))
{
//get the selected parameter off the query string from MainPage.
selected = NavigationContext.QueryString["selected"];
}
//did the querystring indicate we should go to item2 instead of item1?
if (selected == "item2")
{
//item2 is the second item, but 0 indexed.
myPanorama.DefaultItem = myPanorama.Items[1];
}
base.OnNavigatedTo(e);
}
Run Code Online (Sandbox Code Playgroud)
这是一个使用查询字符串的示例应用程序. http://dl.dropbox.com/u/129101/Panorama_querystring.zip
更简单(更好)的想法是全局定义变量,或使用静态类.在App.xaml.cs,定义
using System.Collections.Generic;
public static Dictionary<string,object> PageContext = new Dictionary<string,object>;
Run Code Online (Sandbox Code Playgroud)
然后,在第一页上,简单地做
MyComplexObject obj;
int four = 4;
...
App.PageContext.Add("mycomplexobj",obj);
App.PageContext.Add("four",four);
Run Code Online (Sandbox Code Playgroud)
然后,在新页面上,只需执行
MyComplexObj obj = App.PageContext["mycomplexobj"] as MyComplexObj;
int four = (int)App.PageContext["four"];
Run Code Online (Sandbox Code Playgroud)
为安全起见,您应该检查对象是否存在:
if (App.PageContext.ContainsKey("four"))
int four = (int)App.PageContext["four"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
880 次 |
| 最近记录: |