zoukankan      html  css  js  c++  java
  • Windows Phone 页面之间参数传递方法

    目前对WP7开发正在研究,对页面之间参数传递进行了一个小总结,有不正确的地方,欢迎大家指正。。 WP7编程采用的技术是Silverlight,页面之间参数传递的方式主要有 通过NavigationContext的QueryString方式; 通过程序的App类设置全局变量; 通过PhoneApplicationService类的State属性; 通过NavigationEventArgs事件类的Cont
      

      目前对WP7开发正在研究,对页面之间参数传递进行了一个小总结,有不正确的地方,欢迎大家指正。。

      WP7编程采用的技术是Silverlight,页面之间参数传递的方式主要有

      通过NavigationContext的QueryString方式;

      通过程序的App类设置全局变量;

      通过PhoneApplicationService类的State属性;

      通过NavigationEventArgs事件类的Content属性设置

      1.通过NavigationContext的QueryString函数。

      首先通过NavigationService类进行设置导航至Page1页面。

    NavigationService.Navigate(new Uri("/Page1.xaml?id=1",UriKind.Relative));
     

      在Page1页面的PhoneApplicationPage_Loaded方法中可以通过NavigationContext的QueryString方法获取传递的参数值,如下所示。

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
      int id =  int.Parse(NavigationContext.QueryString["id"]);
    }


     2. 通过程序的App类设置全局变量;

      由于App 类继承自Application类,而通过Application的Current属性可以获取到与当前程序关联的Application类实例,然后通过转换就可以得到App类实例,因此,通过在App类中设置全局变量,在程序的其他任何页面都可以访问。

    public partial class App:Application
    {
           public int ID { getset;}
    }

      假设从页面Page1中需要把参数传递给Page2页面中,可以先在Page1页面中先设置;

    App app = Application.Current as App;  
    app.Id= 1; //设置传递参数;  

      在Page2页面获取设置的参数值;

    App app = Application.Current as App;
    int id = app.ID; //获取在Page1页面传递参数;

      3. 通过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);
    }

     protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
     {
        object myTxt = null;
        if (phoneAppService.State.ContainsKey("id"))
        {
             if (phoneAppService.State.TryGetValue("id"out myTxt))
             {
                  myTextBox.Text = myTxt.ToString();
             }
        }
        base.OnNavigatedTo(e);
     }

      在Page2中获取

    protectedoverridevoidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgse)
    {
    if(PhoneApplicationService.Current.State.ContainsKey("id"))
    {
      myTextBlock.Text=PhoneApplicationService.Current.State["id"as string;
    }
    base.OnNavigatedTo(e);
    }
    protectedoverridevoidOnNavigatedFrom(System.Windows.Navigation.NavigationEventArgse)
    {
    PhoneApplicationService.Current.State["id"]=myTextBlock.Text;
    base.OnNavigatedFrom(e);
    }

      4. 通过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 (param4 != null)
        {
            textBlock3.Text = ID.ToString(); //获取参数值;
        }

      本文来自wangbole的博客,原文地址:http://blog.csdn.net/wangbole/article/details/7174663


  • 相关阅读:
    7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式
    7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值
    Java 代码块:静态代码块、构造代码块、构造函数块
    MyBatis Like
    7 -- Spring的基本用法 -- 9...容器中Bean的生命周期
    my-small.ini、my-medium.ini、my-large.ini、my-huge.ini文件的作用
    java.lang.Class<T> -- 反射机制及动态代理
    7 -- Spring的基本用法 -- 8... 抽象Bean与子Bean;Bean继承与Java继承的区别;容器中的工厂Bean;获得Bean本身的id;强制初始化Bean
    Eclipse Maven 配置setting.xml 的镜像远程仓库
    7 -- Spring的基本用法 -- 7... 创建Bean的3种方式
  • 原文地址:https://www.cnblogs.com/swarb/p/9924284.html
Copyright © 2011-2022 走看看