Windows Phone開發之路(17) 如何在頁面間共享數據


  上一個項目實現的功能是如何從源頁面傳遞數據到目標頁面,但是,當回到源頁面時,如何才能返回數據,實現數據共享呢?這個就是這一篇文章要解決的問題,而且解決這個問題有幾個方案,總結如下。這里共享的數據是頁面背景顏色。

方案一:使用App類來存儲共享數據

  MainPage.xaml XAML代碼:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Navigate to Second Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  MainPage.xaml C#代碼:

namespace SilverlightShareData1
{
public partial class MainPage : PhoneApplicationPage
{
Random rand = new Random();

// 構造函數
public MainPage()
{
InitializeComponent();
}

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//處理Manipulation事件
{
//todo:將MainPage頁面背景保存到App類中聲明的公共屬性中,然后導航到目標頁面
if (this.ContentPanel.Background is SolidColorBrush)
{
(Application.Current as App).SharedColor = (this.ContentPanel.Background as SolidColorBrush).Color;//將當前背景保存到App類的公共屬性中,Application靜態屬性Current返回當前Application對象
}

this.NavigationService.Navigate(new Uri("/SecondPage.xaml",UriKind.Relative));//導航到目標頁面

e.Complete();
e.Handled = true;
}

protected override void OnManipulationStarted(ManipulationStartedEventArgs e)//重寫基類Control(MainPage從它派生)的虛方法
{
//todo:實現點擊屏幕隨機更換背景顏色
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255,(byte)rand.Next(256),(byte)rand.Next(256),(byte)rand.Next(256)));//隨機更換背景顏色

base.OnManipulationStarted(e);//基類訪問表達式調用基類虛方法
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//重寫基類Page的虛方法(當頁面成為框架的活動頁面時發生)
{
//todo:從App類的公共屬性中獲取背景顏色值,然后初始化頁面背景
Color? SharedColor = (Application.Current as App).SharedColor;//訪問App類中的SharedColor屬性

if (SharedColor != null)//注意一定要先判斷是否為空,因為SharedColor是可空類型
{
this.ContentPanel.Background = new SolidColorBrush(SharedColor.Value);
}

base.OnNavigatedTo(e);
}
}
}

  

  SecondPage.xaml XAML代碼:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Go back to Main Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  SecondPage.xaml C#代碼:

namespace SilverlightShareData1
{
public partial class SecondPage : PhoneApplicationPage
{
Random rand = new Random();

public SecondPage()
{
InitializeComponent();
}

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//處理Manipulation事件(當單擊TextBlock的有效區域時發生)
{
//todo:返回到源頁面
this.NavigationService.GoBack();

e.Complete();
e.Handled = true;
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//重寫基類Page的虛方法(當頁面成為框架的活動頁面時發生)
{
//todo:訪問App類中公共屬性,然后用該屬性值初始化頁面背景顏色
Color? SharedColor = (Application.Current as App).SharedColor;//訪問App類中的SharedColor屬性

if (SharedColor != null)//注意一定要先判斷是否為空,因為SharedColor是可空類型
{
this.ContentPanel.Background = new SolidColorBrush(SharedColor.Value);
}

base.OnNavigatedTo(e);
}

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)//重寫基類Page的虛方法(當頁面不再是框架的活動頁面時發生)
{
//todo: 當頁面離開時將當前背景顏色保存到App類中的公共屬性中,以便源頁面在加載時獲取該屬性的值並初始化頁面背景
if (this.ContentPanel.Background is SolidColorBrush)
{
(Application.Current as App).SharedColor = (this.ContentPanel.Background as SolidColorBrush).Color;
}

base.OnNavigatedFrom(e);
}

protected override void OnManipulationStarted(ManipulationStartedEventArgs e)//重寫基類Control(MainPage從它派生)的虛方法(當單擊屏幕時發生)
{
//todo:實現點擊屏幕隨機更換背景顏色
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256)));//隨機更換背景顏色

base.OnManipulationStarted(e);//基類訪問表達式調用基類虛方法
}
}
}

  App.xaml C#代碼:

//用於在頁面間共享數據的公共屬性
public Color? SharedColor { set; get; }//C#3.0自動屬性,Color?表示可空類型

  效果如圖:

     
      導航到目標頁面時              返回到源頁面時

  注意:1,程序中所有頁面都可以訪問到從Application派生的App類。
     2,訪問Application類的靜態屬性Current可以返回當前應用程序的Application對象。
     3,注意Page類的OnNavigatedTo方法和OnNavigatedFrom方法的區別。OnNavigatedTo是當頁面成為框架的活動頁面時調用,而OnNavigatedFrom是當頁面不再(離開)是框架的活動頁面時調用。

方案二:使用頁面中的公共屬性來存儲共享數據

  這個方案的實現原理與方案一差不多,只不過這里將共享數據保存在了頁面中,而不像上例中保存在App這樣的第三方類中。

  MainPage.xaml XAML代碼:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Navigate to Second Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  MainPage.xaml C#代碼:

namespace SilverlightShareData2
{
public partial class MainPage : PhoneApplicationPage
{
Random rand = new Random();

// 構造函數
public MainPage()
{
InitializeComponent();
}

//公共屬性,用來存儲共享數據
public Color? ReturnedColor { set; get; }

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//處理Manipulation事件(當單擊TextBlock是發生)
{
//todo:導航到目標頁面
this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));//導航到目標頁面

e.Complete();
e.Handled = true;
}

protected override void OnManipulationStarted(ManipulationStartedEventArgs e)//重寫基類Control(MainPage從它派生)的虛方法(當單擊屏幕時發生)
{
//todo:實現點擊屏幕隨機更換背景顏色
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256)));//隨機更換背景顏色

base.OnManipulationStarted(e);//基類訪問表達式調用基類虛方法
}

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)//重寫基類Page的虛方法(當頁面不再成為框架的活動頁面時調用)
{
//todo:保存當前頁面背景到目標頁面(在這里是SecondPage)的公共屬性中
if (this.ContentPanel.Background is SolidColorBrush)
{
Color clr = (this.ContentPanel.Background as SolidColorBrush).Color;

if (e.Content is SecondPage)
{
(e.Content as SecondPage).ReturnedSecondColor = clr;
}
}

base.OnNavigatedFrom(e);
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//重寫基類Page的虛方法(當頁面成為框架的活動頁面時調用)
{
//todo:獲取公共屬性的值並初始化頁面背景
if (ReturnedColor != null)
{
this.ContentPanel.Background = new SolidColorBrush(ReturnedColor.Value);
}

base.OnNavigatedTo(e);
}
}
}

  SecondPage.xaml XAML代碼:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Go back to Main Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  SecondPage.xaml C#代碼:

namespace SilverlightShareData2
{
public partial class SecondPage : PhoneApplicationPage
{
Random rand = new Random();

public SecondPage()
{
InitializeComponent();
}

//公共屬性,用來存儲共享數據
public Color? ReturnedSecondColor { set; get; }

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//處理Manipulation事件(當單擊TextBlock時發生)
{
//todo:返回到源頁面
this.NavigationService.GoBack();

e.Complete();
e.Handled = true;
}

protected override void OnManipulationStarted(ManipulationStartedEventArgs e)//重寫基類Control(MainPage從它派生)的虛方法(當單擊屏幕時發生)
{
//todo:實現點擊屏幕隨機更換背景顏色
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256)));//隨機更換背景顏色

base.OnManipulationStarted(e);//基類訪問表達式調用基類虛方法
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//重寫基類Page的虛方法(當頁面成為框架的活動頁面時調用)
{
//todo:獲取公共屬性的值並初始化頁面背景
if (ReturnedSecondColor != null)
{
this.ContentPanel.Background = new SolidColorBrush(ReturnedSecondColor.Value);
}

base.OnNavigatedTo(e);
}

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)//重寫基類Page的虛方法(當頁面不再成為框架的活動頁面時調用)
{
//todo:保存當前頁面背景到源頁面(在這里是MainPage)的公共屬性中
if (this.ContentPanel.Background is SolidColorBrush)
{
Color clr = (this.ContentPanel.Background as SolidColorBrush).Color;

if (e.Content is MainPage)
{
(e.Content as MainPage).ReturnedColor = clr;
}
}

base.OnNavigatedFrom(e);
}
}
}

  效果與方案一的一樣。
  注意:1,OnNavigatedFrom和OnNavigatedTo方法都有一個類型為NavigationEventArgs的事件參數,NavigationEventArgs類型定義了兩個屬性:Uri類型的Uri和Object類型的Content,它們都標識了要導航到的頁面。記住,Content屬性是獲取目標頁面對象的最方便的方式。

方案三:使用PhoneApplicationService對象的State屬性

  MainPage.xaml XAML代碼:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Navigate to Second Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  MainPage.xaml C#代碼:

namespace SilverlightRetainData
{
public partial class MainPage : PhoneApplicationPage
{
// 構造函數
public MainPage()
{
InitializeComponent();
}

//公共屬性保存目標頁面的背景顏色
public Color? ReturnedColor { get; set; }

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//處理Manipulation事件(當單擊TextBlock時發生)
{
//todo:導航到目標頁面
this.NavigationService.Navigate(new Uri("/SecondPage.xaml",UriKind.Relative));

e.Complete();
e.Handled = true;
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//重寫基類Page的虛方法(當頁面成為活動頁面時調用)
{
//todo:獲取公共屬性的值並初始化頁面背景
if (ReturnedColor != null)
{
//Color clr = ReturnedColor.Value;
this.ContentPanel.Background = new SolidColorBrush(ReturnedColor.Value);
}

base.OnNavigatedTo(e);
}
}
}

  SecondPage.xaml XAML代碼:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Go back to Main Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  SecondPage.xaml C#代碼:

namespace SilverlightRetainData
{
public partial class SecondPage : PhoneApplicationPage
{
Random rand = new Random();

public SecondPage()
{
InitializeComponent();
}

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//處理Manipulation事件(當單擊TextBlock時發生)
{
//todo:返回到源頁面
this.NavigationService.GoBack();

e.Complete();
e.Handled = true;
}

protected override void OnManipulationStarted(ManipulationStartedEventArgs e)//重寫基類Control(MainPage從它派生)的虛方法(當單擊屏幕時發生)
{
//todo:隨機改變背景顏色
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255,(byte)rand.Next(256),(byte)rand.Next(256),(byte)rand.Next(256)));

base.OnManipulationStarted(e);
}

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)//重寫基類Page(MainPage從它派生)的虛方法(當頁面離開時調用)
{
//todo:將當前頁面背景顏色保存到MainPage中的公共屬性ReturnedColor中,然后將當前頁面背景顏色保存到PhoneApplicationService對象的State字典中,以便恢復時取回顏色
if (this.ContentPanel.Background is SolidColorBrush)
{
Color clr=(this.ContentPanel.Background as SolidColorBrush).Color;
if (e.Content is MainPage)
{
(e.Content as MainPage).ReturnedColor = clr;
}

//保存顏色
PhoneApplicationService.Current.State["Color"] = clr;
}

base.OnNavigatedFrom(e);
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//重寫基類Page(MainPage從它派生)的虛方法(當頁面成為活動頁面時調用)
{
//todo:取回在PhoneApplicationService對象State字典中保存的顏色並初始化頁面
if (PhoneApplicationService.Current.State.ContainsKey("Color"))
{
Color clr=(Color)PhoneApplicationService.Current.State["Color"];
this.ContentPanel.Background = new SolidColorBrush(clr);
}

base.OnNavigatedTo(e);
}
}
}

 注意:每當在主頁面中通過按下Back按鍵來退出程序,State字典將會從PhoneApplicationService中全部清空。該State字典只適用於存儲程序同一次運行過程中的臨時數據。如果需要在程序多次執行過程之間保存數據,需要使用獨立存儲。下一篇將會總結到。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM