WP7 页面之间参数传递方法

1 篇文章 0 订阅

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

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

  1. 通过NavigationContext的QueryString方式;
  2. 通过程序的App类设置全局变量;
  3. 通过PhoneApplicationService类的State属性;
  4. 通过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(); //获取参数值;
    }
} 





 

 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值