silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了WP7 页面之间参数传递方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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

目前对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(); //获取参数值;
    }
} 





 

大佬总结

以上是大佬教程为你收集整理的WP7 页面之间参数传递方法全部内容,希望文章能够帮你解决WP7 页面之间参数传递方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。