您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 人事档案/员工关系 > WindowsPhone开发教程8
WindowsPhone笔记(8)页面间数据共享通过上一篇笔记我们知道了如何将源页面(调用Navigation函数的页面)的数据传递到目标页面中,但是当我们把这个顺序反过来,即把目标页面的数据返回给源页面时该怎么去做呢?在这篇笔记中我们给出三个解决方案。1.通过App类保存页面共享数据在WindowsPhone笔记中的第一篇笔记中我提到过:App类通常用来存储整个应用程序所使用的资源。该类从Application类中派生,我们通过Application.Current属性可以返回当前应用程序的Application对象,我们可以把理解为当前应用程序的实例,或者说一个全局变量,在各个页面都可以很轻易的访问到它。和以前一样,我们通过一个简单的示例来学习如何使用APP类在页面间共享数据,和上一篇笔记中给出的示例类似,我们首先在APP.xaml.cs也就是为App类中定义一个类型为Color?的属性,用于存储在页面间需要共享的数据:1publicpartialclassApp:Application2{3//用于在页面间共享数据的公共属性,可空类型4publicColor?ShareColor{get;set;}5}复制代码接着是这个示例的第一个页面MainPage.xaml的前端代码:1!--ContentPanel-在此处放置其他内容--2Gridx:Name=ContentPanelGrid.Row=1Margin=12,0,12,03TextBlockPadding=034FontSize=32HorizontalAlignment=CenterName=tblNavigationSecondPageText=NavigationtoSecondPageVerticalAlignment=CenterManipulationStarted=tblNavigationSecondPage_ManipulationStarted/4/Grid复制代码MainPage.xmal.cs后台程序处理代码:1publicpartialclassMainPage:PhoneApplicationPage2{3Randomran=newRandom();4//构造函数5publicMainPage()6{7InitializeComponent();8}910privatevoidtblNavigationSecondPage_ManipulationStarted(objectsender,ManipulationStartedEventArgse)11{12//ContentPanel背景颜色不为默认值时(Brush类null)13if(this.ContentPanel.BackgroundisSolidColorBrush)14{15(Application.CurrentasApp).ShareColor=(ContentPanel.BackgroundasSolidColorBrush).Color;1617this.NavigationService.Navigate(newUri(/SecondPage.xaml,UriKind.Relative));18e.Complete();19e.Handled=true;20}21}2223protectedoverridevoidOnManipulationStarted(ManipulationStartedEventArgse)24{25this.ContentPanel.Background=newSolidColorBrush(Color.FromArgb26(255,(byte)ran.Next(255),(byte)ran.Next(255),(byte)ran.Next(255)));2728base.OnManipulationStarted(e);29}3031//当页面变为框架(Frame)中的活动页面时调用。32protectedoverridevoidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgse)33{34Color?shareColor=(Application.CurrentasApp).ShareColor;35if(shareColor!=null)36{37this.ContentPanel.Background=newSolidColorBrush(shareColor.Value);38}3940base.OnNavigatedTo(e);41}42}43复制代码接着是我们的第二个页面SecondPage.xmal的前端代码:1!--ContentPanel-在此处放置其他内容--2Gridx:Name=ContentPanelGrid.Row=1Margin=12,0,12,03TextBlockPadding=034HorizontalAlignment=CenterName=tblNavigationMainPageText=GoBackToMainPageFontSize=32VerticalAlignment=CenterManipulationStarted=tblNavigationMainPage_ManipulationStarted4/5/Grid复制代码SecondPage.xmal.cs后台程序处理代码:1publicpartialclassSecondPage:PhoneApplicationPage2{3Randomran=newRandom();4publicSecondPage()5{6InitializeComponent();7}89privatevoidtblNavigationMainPage_ManipulationStarted(objectsender,ManipulationStartedEventArgse)10{11//ContentPanel背景颜色不为默认值时(Brush类null)12if(this.ContentPanel.BackgroundisSolidColorBrush)13{14(Application.CurrentasApp).ShareColor=(this.ContentPanel.BackgroundasSolidColorBrush).Color;15}16this.NavigationService.GoBack();17}1819protectedoverridevoidOnManipulationStarted(ManipulationStartedEventArgse)20{21this.ContentPanel.Background=newSolidColorBrush(Color.FromArgb22(255,(byte)ran.Next(255),(byte)ran.Next(255),(byte)ran.Next(255)));23}2425//当页面变为框架(Frame)中的活动页面时调用。26protectedoverridevoidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgse)27{28Color?sharedColor=(Application.CurrentasApp).ShareColor;29if(sharedColor!=null)30{31this.ContentPanel.Background=newSolidColorBrush(sharedColor.Value);32}3334base.OnNavigatedTo(e);35}36}复制代码编译运行程序,我们可以发现当前显示页面都把自己ContentPanel背景色保存到公共属性sharedColor中,通过时当前显示页面的ContentPanel元素的背景色变为上一个显示页面的背景色。下面是程序运行的效果:MainPage——SecondPageSecondPage——MainPage这个示例中我们需要注意的是:OnNavigatedTo方法(当页面变为框架(Frame)活动页面时调用),这个方法的NavigationEventArgs类型参数有两个属性:Uri类型的Uri(将要呈现页面的Uri)object类型的Content(将要呈现页面的实例)2.使用NavigationEventArgs类型参数保存共享数据在这个下面的示例中我们主要通过这个类型的参数的属性来设置源页面和目标页面的ContentPanel元素的背景色属性。Page类除了OnNavigatedTo()方法外,还包括:OnFragmentNavigation方法——在导航到页面上的片段时调用。OnNavigatedFrom方法——当页面不再是框架中的活动页面时调用。OnnavigatingFrom方法——在页面即将不再是框架中的活动页面时调用。我们可以通过利用这些在不同的周期运行的Page类方法来做更多的事情,比如我们利用OnNavigatedFrom方法的特性在源页面导航到目标页面时设置其属性,或调用其方法。下面我们通过一个简单的示例来实现这个目标,该示例的MainPage和SecondPage页面的的前端代码和前面的示例相同,这里只给出两个页面的后台代码:MainPage.xaml.cs:1publicpartialclassMainPage:PhoneApplicationPage2{3//构造函数4publicMainPage()5{6InitializeComponent();7}89publicColor?ReturnedColor{get;set;}//为MainPage类增加的属性,用于存放需要显示的背景色10privatevoidtblNavigationSecondPage_ManipulationStarted(objectsender,ManipulationStartedEventArgse)11{12this.NavigationService.Navigate(newUri(/SecondPage.xaml,UriKind.Relative));1314e.Complete();15e.Handled=true;16}1718//当页面变为框架(Frame)中的活动页面时调用。19protectedoverridevoidOnNavigatedTo(NavigationEventArgse)20{21if(ReturnedColor!=null)22{23this.ContentPanel.Background=newSolidColorBrush(ReturnedColor.Value);24}2526base.OnNavigatedTo(e);27}28}复制代码SecondPage.xaml.cs:1publicpartialclassSecondPage:PhoneApplicationPage2{3Randomran=newRandom();4publicSecondPage()5{6InitializeComponent();7}89privatevoidtblNavigationMainPage_ManipulationStarted(objectsender,ManipulationStartedEventArgse)10{11this.NavigationService.GoBack();1213e.Complete();14e.Handled=true;15}1617protectedoverridevoidOnManipulationStarted(ManipulationStartedEventArgse)18{19this.Co
本文标题:WindowsPhone开发教程8
链接地址:https://www.777doc.com/doc-2856264 .html