Windows Phone笔记(8)页面间数据共享

Windows Phone笔记(8)页面间数据共享

 

  通过上一篇笔记我们知道了如何将源页面(调用Navigation函数的页面)的数据传递到目标页面中,但是当我们把这个顺序反过来,即把目标页面的数据返回给源页面时该怎么去做呢?在这篇笔记中我们给出三个解决方案。

1.通过App类保存页面共享数据

  在Windows Phone笔记中的第一篇笔记中我提到过:App类通常用来存储整个应用程序所使用的资源。该类从Application类中派生,我们通过Application.Current属性可以返回当前应用程序的Application对象,我们可以把理解为当前应用程序的实例,或者说一个全局变量,在各个页面都可以很轻易的访问到它。

  和以前一样,我们通过一个简单的示例来学习如何使用APP类在页面间共享数据,和上一篇笔记中给出的示例类似,我们首先在APP.xaml.cs也就是为App类中定义一个类型为Color?的属性,用于存储在页面间需要共享的数据:

1    public partial class App : Application
2 {
3 //用于在页面间共享数据的公共属性,可空类型
4 public Color? ShareColor { get; set; }
5 }

接着是这个示例的第一个页面MainPage.xaml的前端代码:

复制代码
1         <!--ContentPanel - 在此处放置其他内容-->
2 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
3 <TextBlock Padding="0 34" FontSize="32" HorizontalAlignment="Center" Name="tblNavigationSecondPage" Text="Navigation to SecondPage" VerticalAlignment="Center" ManipulationStarted="tblNavigationSecondPage_ManipulationStarted"/>
4 </Grid>
复制代码

 

MainPage.xmal.cs后台程序处理代码:

复制代码
 1   public partial class MainPage : PhoneApplicationPage
2 {
3 Random ran = new Random();
4 // 构造函数
5 public MainPage()
6 {
7 InitializeComponent();
8 }
9
10 private void tblNavigationSecondPage_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
11 {
12 //ContentPanel背景颜色不为默认值时(Brush类null)
13 if (this.ContentPanel.Background is SolidColorBrush)
14 {
15 (Application.Current as App).ShareColor = (ContentPanel.Background as SolidColorBrush).Color;
16
17 this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
18 e.Complete();
19 e.Handled = true;
20 }
21 }
22
23 protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
24 {
25 this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb
26 (255, (byte)ran.Next(255), (byte)ran.Next(255), (byte)ran.Next(255)));
27
28 base.OnManipulationStarted(e);
29 }
30
31 //当页面变为框架(Frame)中的活动页面时调用。
32 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
33 {
34 Color? shareColor = (Application.Current as App).ShareColor;
35 if (shareColor != null)
36 {
37 this.ContentPanel.Background = new SolidColorBrush(shareColor.Value);
38 }
39
40 base.OnNavigatedTo(e);
41 }
42 }
43
复制代码

 

接着是我们的第二个页面SecondPage.xmal的前端代码:

复制代码
1       <!--ContentPanel - 在此处放置其他内容-->
2 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
3 <TextBlock Padding="0 34" HorizontalAlignment="Center" Name="tblNavigationMainPage" Text="GoBack To MainPage" FontSize="32" VerticalAlignment="Center" ManipulationStarted="tblNavigationMainPage_ManipulationStarted"
4 />
5 </Grid>
复制代码

 

SecondPage.xmal.cs后台程序处理代码:

复制代码
 1    public partial class SecondPage : PhoneApplicationPage
2 {
3 Random ran = new Random();
4 public SecondPage()
5 {
6 InitializeComponent();
7 }
8
9 private void tblNavigationMainPage_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
10 {
11 //ContentPanel背景颜色不为默认值时(Brush类null)
12 if (this.ContentPanel.Background is SolidColorBrush)
13 {
14 (Application.Current as App).ShareColor = (this.ContentPanel.Background as SolidColorBrush).Color;
15 }
16 this.NavigationService.GoBack();
17 }
18
19 protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
20 {
21 this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb
22 (255, (byte)ran.Next(255), (byte)ran.Next(255), (byte)ran.Next(255)));
23 }
24
25 //当页面变为框架(Frame)中的活动页面时调用。
26 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
27 {
28 Color? sharedColor = (Application.Current as App).ShareColor;
29 if (sharedColor != null)
30 {
31 this.ContentPanel.Background = new SolidColorBrush(sharedColor.Value);
32 }
33
34 base.OnNavigatedTo(e);
35 }
36 }
复制代码

编译运行程序,我们可以发现当前显示页面都把自己ContentPanel背景色保存到公共属性sharedColor中,通过时当前显示页面的ContentPanel元素的背景色变为上一个显示页面的背景色。下面是程序运行的效果:

 MainPage——>>>SecondPage                  SecondPage——>>>MainPage

   

这个示例中我们需要注意的是:OnNavigatedTo方法(当页面变为框架(Frame)活动页面时调用),这个方法的NavigationEventArgs类型参数两个属性:

  • Uri类型的Uri(将要呈现页面的Uri)
  • object类型的Content(将要呈现页面的实例)

 2.使用NavigationEventArgs类型参数保存共享数据

   在这个下面的示例中我们主要通过这个类型的参数的属性来设置源页面和目标页面的ContentPanel元素的背景色属性。Page类除了OnNavigatedTo()方法外,还包括:

      • OnFragmentNavigation方法  ——  在导航到页面上的片段时调用。
      • OnNavigatedFrom方法         ——  当页面不再是框架中的活动页面时调用。
      • OnnavigatingFrom方法        ——      在页面即将不再是框架中的活动页面时调用。

我们可以通过利用这些在不同的周期运行的Page类方法来做更多的事情,比如我们利用OnNavigatedFrom方法的特性在源页面导航到目标页面时设置其属性,或调用其方法。下面我们通过一个简单的示例来实现这个目标,该示例的MainPage和SecondPage页面的的前端代码和前面的示例相同,这里只给出两个页面的后台代码:

MainPage.xaml.cs:

复制代码
 1 public partial class MainPage : PhoneApplicationPage
2 {
3 // 构造函数
4 public MainPage()
5 {
6 InitializeComponent();
7 }
8
9 public Color? ReturnedColor { get; set; }//为MainPage类增加的属性,用于存放需要显示的背景色
10 private void tblNavigationSecondPage_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
11 {
12 this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
13
14 e.Complete();
15 e.Handled = true;
16 }
17
18 //当页面变为框架(Frame)中的活动页面时调用。
19 protected override void OnNavigatedTo(NavigationEventArgs e)
20 {
21 if (ReturnedColor != null)
22 {
23 this.ContentPanel.Background = new SolidColorBrush(ReturnedColor.Value);
24 }
25
26 base.OnNavigatedTo(e);
27 }
28 }
复制代码

 

SecondPage.xaml.cs:

复制代码
 1     public partial class SecondPage : PhoneApplicationPage
2 {
3 Random ran = new Random();
4 public SecondPage()
5 {
6 InitializeComponent();
7 }
8
9 private void tblNavigationMainPage_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
10 {
11 this.NavigationService.GoBack();
12
13 e.Complete();
14 e.Handled = true;
15 }
16
17 protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
18 {
19 this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb
20 (255, (byte)ran.Next(255), (byte)ran.Next(255), (byte)ran.Next(255)));
21
22 base.OnManipulationStarted(e);
23 }
24
25 //当页面不再是框架(Frame)中的活动页面时调用。
26 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
27 {
28 Color clr = (this.ContentPanel.Background as SolidColorBrush).Color;
29
30 if (e.Content is MainPage)
31 {
32 //设置MainPage页面的背景色
33 (e.Content as MainPage).ReturnedColor = clr;
34 }
35 base.OnNavigatedFrom(e);
36 }
37 }
复制代码

在这个示例中:

  MainPage页面没有改变颜色的方法,当导航到SecondPage页面时,触摸该页面的TextBlock控件之外的屏幕改变ContentPanel背景色,随后点击TextBlock控件,调用GoBack方法返回到上一页面也就是MainPage中,同时利用OnNavigatedFrom方法,把MainPage的ContentPanel控件的背景色设置和自身一个颜色。

 

3.通过PhoneApplicationService类来保存共享数据

  使用的PhoneApplicationService类在App.xaml中定义:

复制代码
1  <Application.ApplicationLifetimeObjects>
2 <!--处理应用程序的生存期事件所需的对象-->
3 <shell:PhoneApplicationService
4 Launching="Application_Launching" Closing="Application_Closing"
5 Activated="Application_Activated" Deactivated="Application_Deactivated"/>
6 </Application.ApplicationLifetimeObjects>
复制代码

需要引用:Microsoft.Phone.Shell命名空间,PhoneApplicationService类有一个State属性(类型为:IDictionary<string,object>)可以用来作为保存和恢复数据的字典,所有保存在State字典中的对象都必须是可序列化的(对象可以转换为XML,并可以从XML中反序列化为对象)。并且需要注意的是,保存的数据只有在应用程序运行的时候才会保留(临时数据,transient),并不适合必须在多次执行期间保存的应用程序的配置信息,这里我们可以考虑使用独立存储(将在后面的笔记中介绍它)。

  下面我们通过一个简单的示例来学习使用PhoneApplicationService类保存共享数据的使用方法。这个示例的前端显示XAML代码和前面的示例一致,这里就不在给出,下面直接给出后台处理程序代码:

MainPage.xaml.cs:

复制代码
 1 public partial class MainPage : PhoneApplicationPage
2 {
3 // 构造函数
4 public MainPage()
5 {
6 InitializeComponent();
7 }
8 Random ran = new Random();
9
10 private void tblNavigationSecondPage_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
11 {
12 this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
13
14 e.Complete();
15 e.Handled = true;
16 }
17
18 //当页面变为框架(Frame)活动页面时调用
19 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
20 {
21 //取回颜色
22 if (PhoneApplicationService.Current.State.ContainsKey("Color"))
23 {
24 Color clr = (Color)PhoneApplicationService.Current.State["Color"];
25 this.ContentPanel.Background = new SolidColorBrush(clr);
26 }
27 base.OnNavigatedTo(e);
28 }
29 }
复制代码

SecondPage.xaml.cs:

复制代码
 1  public partial class SecondPage : PhoneApplicationPage
2 {
3 public SecondPage()
4 {
5 InitializeComponent();
6 }
7
8 Random ran = new Random();
9
10 //随机改变背景色
11 protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
12 {
13 this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb
14 (255, (byte)ran.Next(255), (byte)ran.Next(255), (byte)ran.Next(255)));
15
16 base.OnManipulationStarted(e);
17 }
18
19 //当页面不再是框架中的活动页面时调用
20 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
21 {
22 //如果ContentPanel背景色不为默认值(null,Brush)
23 if (this.ContentPanel.Background is SolidColorBrush)
24 {
25 Color clr = (this.ContentPanel.Background as SolidColorBrush).Color;
26
27 //保存颜色
28 PhoneApplicationService.Current.State["Color"] = clr;
29 }
30
31 base.OnNavigatedFrom(e);
32 }
33
34 private void tblNavigationMainPage_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
35 {
36 this.NavigationService.GoBack();
37
38 e.Complete();
39 e.Handled = true;
40 }
41
42 }
复制代码

编译运行程序,先导航到SecondPage页面,改变背景色,然后在导航到MainPage页面中(背景色改变),下面是实际效果:

 

参考资料:

  http://msdn.microsoft.com/zh-cn/library/ms611629(v=vs.92).aspx

  《Programming Windows Phone 7 Microsoft Silverlight Edition》

作者:晴天猪

出处:http://www.cnblogs.com/IPrograming

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/zhanghaifeng/p/2480919.html