本节实现的内容是数据共享,实现的效果描述:首先是建立两个页面,当页面MainPage通过事件导航到页面SecondPage是,我们需要将MainPage中的一些内容(比如一个字符串)传递到SecondPage中,SecondPage页面就出呈现出传递来的内容,当页面SecondPage通过事件导航到页面MainPage的时候,我们也把一些内容(比如一个字符串)传递与页面MainPage;
代码如下 | 复制代码 |
using Microsoft.Phone.Shell;
MainPage的隐藏文件的全部代码如下:
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); //} } } |
代码如下 | 复制代码 |
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); //} |