目前对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 { get; set;}

}

  假设从页面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 { get; set; }

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

Windows Phone 页面之间参数传递方法的更多相关文章

  1. js jquery 实现html页面之间参数传递(单一参数、对象参数传递)

    最近自己在忙着做毕业设计,后台程序员,前端菜鸡,因为需要,所以实现了html页面之间参数传递.------jstarseven .菜鸡的自我修养. 页面A代码如下: <!DOCTYPE html ...

  2. 浅谈MVC页面之间参数传递

    关于MVC页面之间的传值,有多种方式,下面,我们就Html.RenderAction 方式 和 Html.RenderPartial 方式 来给大家分享一下有什么不同. 一.Html.RenderAc ...

  3. asp.net页面之间传值方法详解

    asp.net中页面之间传值我们用得最多的就是get,post这两种了,其它的如session,appliction,cookie等这些相对来说少用也不是常用的,只是在特殊情况下在使用了. 1. Ge ...

  4. asp.net页面与页面之间参数传递

    传值asp文件send.aspx 代码如下 复制代码 <form id="form1" runat="server" action="recei ...

  5. html中 iframe子页面 与父页面之间的方法调用 ;

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. iframe 父页面与子页面之间的方法的相互调用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. wx小程序自定义组件与页面之间参数传递

    在开发中,很多页面中会使用相同的组件,这时可以将具有相同信息的部分封装成一个组件,方便开发中调用.在调用中可能会涉及到数据的传递问题,例如页面与组件,组件与组件直接的数据传递. 首先看看页面与组件直接 ...

  8. iframe 父页面与子页面之间的方法的相互调用【转】

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Javascript页面之间参数传递 (前端)

    一.来源:tongfang [系统管理员] --[系统管理] 的"SysLeftNavView.ascx.cs 用户插件 usercontrol 左侧菜单导航: <li>< ...

随机推荐

  1. PAT甲级 1112 Stucked Keyboard

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960 这道题初次写的时候,思路也就是考虑 ...

  2. 机器学习:正态方程 python实现

    目录 前言 一.算法介绍 二.核心算法 1. 公式 2.python实现 总结 前言 使用python简单实现机器学习中正态方程算法. 一.算法介绍 与梯度下降算法相比,正态方程同样用于解决最小化代价 ...

  3. 轻松上手SpringBoot+SpringSecurity+JWT实RESTfulAPI权限控制实战

    前言 我们知道在项目开发中,后台开发权限认证是非常重要的,springboot 中常用熟悉的权限认证框架有,shiro,还有就是springboot 全家桶的 security当然他们各有各的好处,但 ...

  4. NOIP 模拟 $11\; \rm biology$

    题解 首先对 \(a\) 离散化,则可推出转移方程 \[dp_{i,j}=\max\{{dp_{{i^{'}},{j^{'}}}+|i-i^{'}|+|j-j^{'}|}\}+b_{i,j} \;\; ...

  5. nacos config基本使用

    说明 Nacos is an easy-to-use dynamic service discovery, configuration and service management platform ...

  6. jpa中使用Query判断条件查询

    jpa中使用Query判断条件查询 @Query(value = " select m.* from mining_area as m " + " where 1 = 1 ...

  7. MySQL主从复制与Atlas读写分离

    配置主从复制 1. 增加主从配置 # 主库配置文件 server-id = 1 log-bin = /var/lib/mysql/mysql-bin expire_logs_days = 10 ski ...

  8. PHP随手记2--获取随机n位不重复字符

    定义一个函数返回26英文字母中n位不重复随机字符 基本思路是利用内置函数生成随机数,取出该位置字母之后将其删除,再进行下一次随机,最后实现字符串拼接就ok! 代码很简单,通俗易懂,直接上代码吧: 1 ...

  9. bicabo C#多线程详解(三)

    继续上一节的问题:调换两个新创建的线程启动顺序会是什么结果? using System; using System.Threading;namespace Test{    class TestThr ...

  10. linux &和&&,|和||

    &和&&,|和||区别: &  表示任务在后台执行,如要在后台运行redis-server,则有  redis-server & && 表示前一 ...