本 節實現的內容是數據共享,實現的效果描述:首先是建立兩個頁面,當頁面MainPage通過事件導航到頁面SecondPage是,我們需要將 MainPage中的一些內容(比如一個字符串)傳遞到SecondPage中,SecondPage頁面就出呈現出傳遞來的內容,當頁面 SecondPage通過事件導航到頁面MainPage的時候,我們也把一些內容(比如一個字符串)傳遞與頁面MainPage;
在建立的MainPage.xaml文件中我只添加了一個Button元素,設置顯示的Content內容,並定義了該元素的觸摸事件:
MainPage的隱藏文件首先需要引用如下命名空間
using Microsoft.Phone.Shell;
MainPage的隱藏文件的全部代碼如下:
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();
}
/// <summary>
/// 點擊導航到第二個頁面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
///// <summary>
///// 接受傳遞的值
///// </summary>
///// <param name="e"></param>
//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的隱藏文件也需要引用相同的命名空間:
using Microsoft.Phone.Shell;
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();
}
/// <summary>
/// 接受傳遞的值
/// </summary>
/// <param name="e"></param>
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);
}
/// <summary>
/// 導航到第一個頁面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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方法,所以 我們在
通 過App類來進行共享參數是因爲在程序中所有的頁面都可以訪問Application派生的App類,在App類中設置的屬性當然我們也可以訪問,其中 Application類的靜態屬性Current返回的是Application對象的引用,如果想轉換爲派生的App類,強制轉換即可
PhoneApplicationService類的實例化在App.xaml文件中
x:Class="ShareData.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
<!--應用程序資源-->
<Application.Resources>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--處理應用程序的生存期事件所需的對象-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>
頁面最後加載的方法:OnNavigatedFrom方法;獲取導航目標頁:e.Content 並進行強制轉換;訪問App類的公共屬性;PhoneApplicationService.State進行暫時的保存及讀取;