WinPhone 开发(1)-----在 XAML 页面之间浏览和数据的传递、保留以及恢复

  使用控件HyperlinkButton实现XAML页面之间的浏览,使用方法:设置HyperlinkButton的NavigateUri为"/ProjectName;component/folder/PageName.xaml" ,其中ProjectName、PageName根据实际情况来改变。如果有XAML页面时建立在一个文件夹里面,那么就要加上文件名字folder。
  换而言之,利用HyperlinkButton导航到另一个XAML页面把NavigateUri设置为"/工程名;component/XAML页面的相对路径"。

  数据的传递:使用QueryString获取NavigateUri中的键值对。
  
  demo:NavigateUri="/Testnavigation;component/Views/Page1.xaml=id=1"

  

1  private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
2 {
3 string id = "";
4 if (NavigationContext.QueryString.TryGetValue("id", out id))
5 {
6 textBox1.Text = String.Format("Value:{0}", id);
7 }
8 }


  数据的保留与恢复:重写方法

 1 PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
2 // retain the value of the textbox
3 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)

4 {
5 phoneAppService.State["myValue"] = textBox1.Text;
6 base.OnNavigatedFrom(e);
7 }
8 // try to get the value which is retained
9 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

10 {
11 object someObject;
12 if (phoneAppService.State.ContainsKey("myValue"))
13 {
14 if (phoneAppService.State.TryGetValue("myValue", out someObject))
15 {
16 textBox1.Text = someObject.ToString();
17 }
18 }
19 base.OnNavigatedTo(e);
20 }





一步一脚印,自己的路自己走


posted @ 2012-02-27 21:21  Nereus_37  阅读(1089)  评论(0编辑  收藏  举报