设为首页 收藏本站
查看: 973|回复: 0

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

[复制链接]

尚未签到

发表于 2015-5-23 09:49:51 | 显示全部楼层 |阅读模式
  通过上一篇笔记我们知道了如何将源页面(调用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         
2         
3            
4         
  
  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       
2         
3            
5         
  
  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
DSC0000.jpg DSC0001.jpg    DSC0002.jpg DSC0003.jpg
  这个示例中我们需要注意的是: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  
2         
3         
6     
  需要引用:Microsoft.Phone.Shell命名空间,PhoneApplicationService类有一个State属性(类型为:IDictionary)可以用来作为保存和恢复数据的字典,所有保存在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页面中(背景色改变),下面是实际效果:
DSC0004.png DSC0005.png
  
  参考资料:
  http://msdn.microsoft.com/zh-cn/library/ms611629(v=vs.92).aspx
  《Programming Windows Phone 7 Microsoft Silverlight Edition》

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

  Windows Phone开发者交流群:79339880,欢迎大家来一起讨论交流,共同学习进步。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69729-1-1.html 上篇帖子: Windows 8 开发31日-第03日-启动画面 下篇帖子: windows 8上使用SmtpClient.Send()发送邮件失败的原因分析。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表