9404803 发表于 2015-5-23 05:13:56

windows phone 三种数据共享的方式(8)


  本节实现的内容是数据共享,实现的效果描述:首先是建立两个页面,当页面MainPage通过事件导航到页面SecondPage是,我们需要将MainPage中的一些内容(比如一个字符串)传递到SecondPage中,SecondPage页面就出呈现出传递来的内容,当页面SecondPage通过事件导航到页面MainPage的时候,我们也把一些内容(比如一个字符串)传递与页面MainPage;
  在建立的MainPage.xaml文件中我只添加了一个Button元素,设置显示的Content内容,并定义了该元素的触摸事件:


  MainPage的隐藏文件首先需要引用如下命名空间


//引用命名空间--PhoneApplicationService类用到
using Microsoft.Phone.Shell;  MainPage的隐藏文件的全部代码如下:

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
//引用命名空间--PhoneApplicationService类用到
using Microsoft.Phone.Shell;
namespace ShareData
{
    public partial class MainPage : PhoneApplicationPage
    {
      // 构造函数
      public MainPage()
      {
            InitializeComponent();
      }
      ///
      /// 点击导航到第二个页面
      ///
      ///
      ///
      private void btn_Click(object sender, RoutedEventArgs e)
      {
            this.NavigationService.Navigate(new Uri("/SecondPage.xaml",UriKind.Relative));
      }
       //知识点①
      protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
      {
            //目标页--知识点②
            if (e.Content is SecondPage)
            {
                ((SecondPage)e.Content).ApplicationTitle.Text = "传递数据成功!";
            }
            //获得application对象的引用--知识点③
            (Application.Current as App).shareData = "通过APP类的属性共享数据";
            //应用程序的状态管理---知识点④
            PhoneApplicationService.Current.State["Share"] = "临时数据";
            base.OnNavigatedFrom(e);
      }
      /////
      ///// 接受传递的值
      /////
      /////
      //protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
      //{
      //    //获得App类中的共享数据
      //    PageTitle.Text = (Application.Current as App).shareData.ToString();
      //    if (PhoneApplicationService.Current.State.ContainsKey("Share"))
      //    {
      //      //获得phoneapplicationService对象中设置state属性
      //      PageTitle.Text += "\n" + PhoneApplicationService.Current.State["Share"].ToString();
      //    }
      //    base.OnNavigatedTo(e);
      //}
    }
}  
  
  
  
  在建立的SecondPage.xaml文件中我只添加了一个Button元素,设置显示的Content内容,并定义了该元素的触摸事件:



  SecondPage的隐藏文件也需要引用相同的命名空间:


//引用命名空间--PhoneApplicationService类用到
using Microsoft.Phone.Shell;  


SecondPage的隐藏文件的全部代码如下:
View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
//引用命名空间--PhoneApplicationService类用到
using Microsoft.Phone.Shell;
namespace ShareData
{
    public partial class SecondPage : PhoneApplicationPage
    {
      public SecondPage()
      {
            InitializeComponent();
      }
      ///
      /// 接受传递的值
      ///
      ///
      protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
      {
            //获得App类中的共享数据
            PageTitle.Text = (Application.Current as App).shareData.ToString();
            if (PhoneApplicationService.Current.State.ContainsKey("Share"))
            {   
                //获得phoneapplicationService对象中设置state属性
                PageTitle.Text += "\n" + PhoneApplicationService.Current.State["Share"].ToString();
            }
            base.OnNavigatedTo(e);
      }
      ///
      /// 导航到第一个页面
      ///
      ///
      ///
      private void btn_Click(object sender, RoutedEventArgs e)
      {
            this.NavigationService.GoBack(); ;
      }

      //protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
      //{
      //    if (e.Content is SecondPage)
      //    {
      //      ((SecondPage)e.Content).PageTitle.Text = "传递数据成功!";
      //    }
      //    (Application.Current as App).shareData = "通过APP类的属性共享数据";
      //    PhoneApplicationService.Current.State["Share"] = "临时数据";
      //    base.OnNavigatedFrom(e);
      //}
  
  我们三种传递参数的中的一种是利用App类下设置属性进行多个页面共享,直接在App类中设置的公共属性:



//设置共享的数据
      public string shareData { get; set; }  至此全部代码已经呈现在这里,那我们是怎么进行数据共享的那,ok,详情如下:首先我们点击MainPage中的button事件,因为没有其他需要执行,此是件执行完毕后会立即执行MainPage页面中的OnNavigatedFrom方法,我们可以看到OnNavigatedFrom的参数是System.Windows.Navigation.NavigationEventArgs就是我们导航是的参数,OnNavigatedFrom方法执行完毕后就会导航到SecondPage页,SecondPage的隐藏文件首先加载的就是构造函数,然后就是OnNavigatedTo方法,所以我们在

OnNavigatedTo方法中接受传递来的数据;同样的原理我们可以把SecondPage中的数据传递到MainPage中,代码中注释掉的部分就是实现该内容,其中MainPage隐藏文件中注释掉的部分,如果去除注释,在运行的时候就会报错,因为在程序进入MainPage页面并触发Button事件后会执行OnNavigatedFrom方法,而此时并没有内容传递;
  

在此重写OnNavigatedFrom的方法是为了在此页面变为非活动页面的时候最后执行,所以有些操作可以在本页面进行完成;像我们这里的可以赋值于目标页面中TextBlock元素中的Text属性;
  

System.Windows.Navigation.NavigationEventArgs 参数就是记录了我们在点击导航时的一些信息,其中e.Content表示我们要导航到的页面,像这样((SecondPage)e.Content).ApplicationTitle.Text = "传递数据成功!";我们也访问目标也的属性
  
  通过App类来进行共享参数是因为在程序中所有的页面都可以访问Application派生的App类,在App类中设置的属性当然我们也可以访问,其中Application类的静态属性Current返回的是Application对象的引用,如果想转换为派生的App类,强制转换即可



  PhoneApplicationService类的实例化在App.xaml文件中

View Code


   
   
   
   
      
      
   
  

PhoneApplicationService类所提供的是对数据的暂时存储,只有在运行的时候才能保留(独立存储空间是可以永久保留的),这个类的属性State可以设置的是字典容器,但是保存在State字典中的对象都必须是可序列化的,可序列化的意思是可以将对象转换成XML,XML也可以从中反序列化成对象   
  页面最后加载的方法:OnNavigatedFrom方法;获取导航目标页:e.Content 并进行强制转换;访问App类的公共属性;PhoneApplicationService.State进行暂时的保存及读取;

  源码下载
页: [1]
查看完整版本: windows phone 三种数据共享的方式(8)