windows phone7 学习笔记03——数据传递

    综合很多资料,参数传递主要有四种方式:

    1、通过NavigationContext的QueryString方式;
    2、通过程序的App类设置全局变量(此方法可以传递对象);
    3、通过NavigationEventArgs事件类的Content属性设置;
    4、通过PhoneApplicationService类的State属性。
 
    1、通过NavigationContext的QueryString方式
    这种传参数方式最简单最易理解,类似于web中的?id=1类型。
    首先在A页面挑战到B页面的uri中加入参数,如“/View/Music.xaml?id=1”;
    在B页面就可以接收了,如
        int id =  int.Parse(NavigationContext.QueryString["id"]);
    注意:(1)一般在接收之前要判断参数是否存在。
         (2)同时传递多个参数可以使用“&”连接。
         (3)地址别命中也可以使用这种方法,使用“{}”来表示变量。如
              <nav:UriMapping Uri="Music/{id}" MappedUri="/View/Music.xaml?id={id}"></nav:UriMapping>
这样设置后“Music/1”就相当于"/View/Music.xaml?id=1"。

    2、通过程序的App类设置全局变量(此方法可以传递对象);
    由于App 类继承自Application类,而通过Application的Current属性可以获取到与当前程序关联的Application类实例,然后通过转换就可以得到App类实例,因此,通过在App类中设置全局变量,在程序的其他任何页面都可以访问。
    (1)在App.xaml的App类中设置全局变量:
        public partial class App : Application
            {
                 Public int ID { get; set; }
            }
    (2)在page1中设置:
        App app = Application.Current as App;
        app.ID = 1;
    (3)在page2中接收:
        App app = Application.Current as App;
        int id = app.ID;
 
    补充:这种方法也可以通过App类的静态属性来传递对象:
    (1)首先我们新建一个MusicClass类:
           public class MusicClass
              {
                   public string Name { set; get; }
                   public string File { set; get; }
              }
    (2)在App类中添加静态属性:
           public static MusicClass Music { get; set; }
    (3)page1中设置参数:
           App.Music = new MusicClass
             {
                File = "song1.mp3",
                Name = "歌曲1"
             };
    (4)page2中接收参数:
           App.Music = new MusicClass
           string musicFile = App.Music.File;
           string musicName = App.Music.Name;
 
    3、通过NavigationEventArgs事件类的Content属性设置;
    在导航至其他页面函数OnNavigatedFrom中,测试导航的目标页面是否为自己真正要转向传递参数的页面,如果是,可以通过NavigationEventArgs事件类的向目标页面注入一些"传递内容"。
           protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)

       {
           var targetPage = e.Content as Page2;
           if (targetPage!=null)
           {
               targetPage.ID= 1;
           }
       }

    然后page2取出参数值:
         public int ID { getset; }

     protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
     {
         if (ID!= null)
         {
             int id = ID.ToString();
         }

     } 

 
    4、通过PhoneApplicationService类的State属性。
    由于PhoneApplicationService类的State是一个IDictionary类型,因此,可以存储任何对象,不过这个对象必须是可序列化(serializable)的。
       注:PhoneApplicationService类,需要访问命名空间using Microsoft.Phone.Shell;
       在程序中,可以不需要自己创建PhoneApplicationService的实例,通过PhoneApplicationService的静态属性Current就可以获取到已有的PhoneApplicationService实例。

        page1中设置参数:
              protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
         {
             phoneAppService.State["id"] = int.Parse(myTextBox.Text);//获取Page1页面的值,进行传递;  
             base.OnNavigatedFrom(e); 
         }

     page2中取出参数:
               protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgse)  
        {
            if(PhoneApplicationService.Current.State.ContainsKey("id")
              {
                     myTextBlock.Text=PhoneApplicationService.Current.State["id"as string;  
              }
              base.OnNavigatedTo(e);  
        }

       注:一般不用这种方法来传递参数,这种方法用的比较多的是记住页面当前的状态,跳转回来后显示刚才正在编辑的内容。

原文地址:https://www.cnblogs.com/zhangkai2237/p/2348402.html