现在的位置: 首页 > 综合 > 正文

WP7 页面之间参数传递方法

2012年05月10日 ⁄ 综合 ⁄ 共 2586字 ⁄ 字号 评论关闭

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





 

 



抱歉!评论已关闭.