当前位置:编程学习 > wap >>

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      &nbs

补充:移动开发 , Windows Phone ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,