天天看點

windows phone 三種資料共享的方式(8)

 本 節實作的内容是資料共享,實作的效果描述:首先是建立兩個頁面,當頁面MainPage通過事件導航到頁面SecondPage是,我們需要将 MainPage中的一些内容(比如一個字元串)傳遞到SecondPage中,SecondPage頁面就出呈現出傳遞來的内容,當頁面 SecondPage通過事件導航到頁面MainPage的時候,我們也把一些内容(比如一個字元串)傳遞與頁面MainPage;

在建立的MainPage.xaml檔案中我隻添加了一個Button元素,設定顯示的Content内容,并定義了該元素的觸摸事件:

<Button x:Name="btn" Content="導航到第二個頁面" Grid.Row="1" Click="btn_Click"></Button>

 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;

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内容,并定義了該元素的觸摸事件:

<Button x:Name="btn" Content="導航到第1個頁面" Grid.Row="1" Click="btn_Click"></Button>

 SecondPage的隐藏檔案也需要引用相同的命名空間:

SecondPage的隐藏檔案的全部代碼如下:

    public partial class SecondPage : PhoneApplicationPage

        public SecondPage()

        /// 接受傳遞的值

        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);

        /// 導航到第一個頁面

            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檔案中

<Application 

    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進行暫時的儲存及讀取;

本文轉自shenzhoulong  51CTO部落格,原文連結:http://blog.51cto.com/shenzhoulong/832671,如需轉載請自行聯系原作者