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对象。
- Windows phone 8 页面间数据共享方式
- windows phone:页面间数据共享
- Windows Phone 7 三种共享数据的方式
- windows phone:页面间传递数据
- windows phone 三种数据共享的方式(8)
- windows phone 8 使用页面传对象的方式 实现页面间的多值传递
- Windows Phone 7 编成之- 6.3 页面之间数据共享和传递
- windows phone 页面之间传值方式
- Windows Phone 页面间跳转
- Windows Phone 的数据存储方式总结
- Windows Phone 的数据存储方式总结
- windows phone 7 页面间跳转方法
- 技术分享 - Windows Phone 的数据存储方式总结
- windows phone挂起时的数据保存方式
- Windows phone 7 图片共享
- windows phone:Windows Phone 的数据
- windows phone 数据传递
- windows phone 数据传递
- WINDOWS API函数大全
- powerdesigner 概念模型转化成物理模型
- Animation之震动效果动画在登录Activity上的应用
- 认识SD卡家族(SD miniSD microSD TF SDIO)
- 第十周项目5-谁是傻子?
- Windows phone 8 页面间数据共享方式
- ML: 斯坦福大学公开课 :机器学习课程
- 如何获取某个进程的主窗口以及创建进程的程序名?
- 开发附带NPAPI插件的Chrome扩展
- 在论坛中出现的比较难的sql问题:3(row_number函数 分组查询)
- 软件项目管理案例分析
- Visual Studio最常用、最高效的快捷键
- 每天写一点代码----双栈实现队列
- Cocos2d-x笔记(四)CCHttpCleint网络请求