Windows phone 8 页面间数据共享方式

来源:互联网 发布:greenplum导出数据库 编辑:程序博客网 时间:2024/05/16 06:43

网上转的,方便自己以后学习。

 页面间传递数据包括两个问题
1、如何从源页面传递数据到目标页面?
下面例子提供解决上面问题的方案。
源页面MainPage.xaml内容区域包含一个TextBlock,如

<TextBlock HorizontalAlignment="Center" Name="txt1" Text="navigate to 2nd page" VerticalAlignment="Center" ManipulationStarted="txt1_ManipulationStarted" />MainPage.xaml.cs代码如下所示:namespace PhoneApp2{public partial class MainPage : PhoneApplicationPage{Random rand = new Random();// 构造函数public MainPage(){InitializeComponent();}private void txt1_ManipulationStarted(object sender, ManipulationStartedEventArgs e){String destination = "/Page1.xaml";if (this.ContentPanel.Background is SolidColorBrush){Color clr = (this.ContentPanel.Background as SolidColorBrush).Color;destination += String.Format("?Red={0}&Green={1}&Blue={2}",clr.R,clr.G,clr.B);}this.NavigationService.Navigate(new Uri(destination, UriKind.Relative));//导航至指定页面e.Complete();e.Handled = true;}protected override void OnManipulationStarted(ManipulationStartedEventArgs e){//当触摸到页面里textblock以外的部分时,contentpanel的背景会变成随机颜色。this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));//设置背景颜色base.OnManipulationStarted(e);}}}目标页面Page1.xaml代码重写了OnNavigatedTo方法,如下所示:protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//当该函数被调用时,页面的构造函数已经执行完毕,但是还没有执行其他的方法{IDictionary<String, String> parameters = this.NavigationContext.QueryString;if (parameters.ContainsKey("Red")){byte R = Byte.Parse(parameters["Red"]);byte G = Byte.Parse(parameters["Green"]);byte B = Byte.Parse(parameters["Blue"]);this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255,R,G,B));}base.OnNavigatedTo(e);}



运行程序,从MainPage导航到Page1,你会发现背景颜色是相同的。
2、当回到源页面时,如何返回数据?
windows phone为我们提供了专门解决第一个问题的方案,但是还没有解决第二个问题的现成方案。下面例子是一个折中的方案,可参考。
上面MainPage.xaml.cs可改成

namespace PhoneApp2{public partial class MainPage : PhoneApplicationPage{Random rand = new Random();public Color? ReturnedColor{set;get;}//这是一个可空的(nullable)Color对象,用来保存想要返回的数据(颜色)// 构造函数public MainPage(){InitializeComponent();}private void txt1_ManipulationStarted(object sender, ManipulationStartedEventArgs e){String destination = "/Page1.xaml";this.NavigationService.Navigate(new Uri(destination, UriKind.Relative));//导航至指定页面e.Complete();e.Handled = true;}protected override void OnManipulationStarted(ManipulationStartedEventArgs e){//当触摸到页面里textblock以外的部分时,contentpanel的背景会变成随机颜色。this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));//设置背景颜色base.OnManipulationStarted(e);}protected override void OnNavigateTo(NavigationEventArgs e)//当构造函数执行完毕且还没有执行其他函数时(自动)调用{if(this.ReturnedColor != null){this.ContentPanel.Background = new SolidColorBrush(this.ReturnedColor.Value);}base.OnNavigateTo(e);} }}


Page1.xaml.cs可改成
namespace PhoneApp2{public partial class Page1 : PhoneApplicationPage{Random rand = new Random();public Color? ReturnedColor{set;get;}//这是一个可空的(nullable)Color对象,用来保存想要返回的数据(颜色)// 构造函数public Page1(){InitializeComponent();}private void txt2_ManipulationStarted(object sender, ManipulationStartedEventArgs e){this.NavigationService.Goback();//导航返回e.Complete();e.Handled = true;}protected override void OnManipulationStarted(ManipulationStartedEventArgs e){//当触摸到页面里textblock以外的部分时,contentpanel的背景会变成随机颜色。this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));//设置背景颜色base.OnManipulationStarted(e);}protected override void OnNavigateFrom(NavigationEventArgs e){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;//在导航出Page1页面时设置返回值并保存到MainPage变量中}base.OnNavigateFrom(e);}}}


-参数NavigationEventArgs类定义了两个属性:Uri类型的Uri和object类型的Content。
-当MainPage使用参数"/Page1.xaml"来调用Navigate()函数时,MainPage中的OnNavigateFrom()方法被调用,调用时参数Uri属性为"/Page1.xaml",Content属性类型为Page1。这是一人新创建的Page1实例。随后Page1调用的OnNavigateTo()也使用同样的事件参数来标识值为"/Page1.xaml"的Uri对象以及值为Page1的Content对象。

原创粉丝点击