Windows Phone开发之路(16) 如何在页面间传递数据
2012-02-11 15:56
435 查看
这一篇文章要解决的问题是如何从源页面传递数据到目标页面。其实Windows Phone已经为我们提供了一套解决方案,那就是查询字符串。
下面这个项目要实现的效果是:当从MainPage页面导航到SecondPage时,该项目为SecondPage提供了MainPage当前的背景色,而且SecondPage也把自己初始化成这种颜色。这里传递的数据是背景颜色值。
MainPage.xaml XAML代码:
MainPage.xaml C#代码:
SecondPage.xaml XAML代码:
SecondPage.xaml C#代码:
效果如图:
下一篇将要总结的是关于如何在页面间数据共享。
下面这个项目要实现的效果是:当从MainPage页面导航到SecondPage时,该项目为SecondPage提供了MainPage当前的背景色,而且SecondPage也把自己初始化成这种颜色。这里传递的数据是背景颜色值。
MainPage.xaml XAML代码:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Text="Navigate to Second Page!" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="0,34" ManipulationStarted="TextBlock_ManipulationStarted"/> </Grid>
MainPage.xaml C#代码:
namespace SilverlightPassData { public partial class MainPage : PhoneApplicationPage { Random rand = new Random(); // 构造函数 public MainPage() { InitializeComponent(); } private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//处理Manipulation事件 { //todo:导航到Second Page页面,并且传递页面背景颜色信息给Second Page. string destination = "/SecondPage.xaml";//创建一个String变量保存目标页面地址 if (this.ContentPanel.Background is SolidColorBrush)//检查能否成功转换成SolidColorBrush类型。Background属性为Brush类型 { Color clr = (this.ContentPanel.Background as SolidColorBrush).Color;//将Background属性值转换成SolidColorBrush类型并获取颜色值 destination += string.Format("?Red={0}&Green={1}&Blue={2}",clr.R,clr.G,clr.B);//将颜色值作为参数附在目标地址后,类似于HTML查询字符串格式 } this.NavigationService.Navigate(new Uri(destination,UriKind.Relative));//导航到目标地址 e.Complete();//表示不再处理其它Manipulation事件 e.Handled = true;//将路由事件标记为已处理 } protected override void OnManipulationStarted(ManipulationStartedEventArgs e)//重写基类Control的虚方法 { //todo:实现当单击屏幕的时候随机改变页面背景色 this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255,(byte)rand.Next(256),(byte)rand.Next(256),(byte)rand.Next(256))); base.OnManipulationStarted(e);//基类访问表达式调用基类方法 } } }
SecondPage.xaml XAML代码:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Text="Go back to Main Page!" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="0,34" ManipulationStarted="TextBlock_ManipulationStarted"/> </Grid>
SecondPage.xaml C#代码:
namespace SilverlightPassData { public partial class SecondPage : PhoneApplicationPage { public SecondPage() { InitializeComponent(); } private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//处理Manipulation事件 { //todo:返回或导航到MainPage页面 this.NavigationService.GoBack(); e.Complete(); e.Handled = true; } protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//重写基类Page的虚方法,当页面变为框架的活动页面时调用 { //todo:获取从MainPage传递过来的数据,并以此来初始化页面背景颜色 IDictionary<string, string> parameters = this.NavigationContext.QueryString;//创建泛型接口字典来接受返回结果对象 if (parameters.ContainsKey("Red"))//调用ContainsKey()方法判断结果对象中是否包含Red的键 { //todo:分别获取每个键的值并转换为byte类型 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);//调用基类虚方法 } } }
效果如图:
下一篇将要总结的是关于如何在页面间数据共享。
相关文章推荐
- Windows Phone开发之路(17) 如何在页面间共享数据
- IOS开发---菜鸟学习之路--(十八)-利用代理实现向上一级页面传递数据
- MVC 如何向页面传递数据
- windows phone 学习之页面导航和数据传递
- android开发:activity之间如何传递bitmap数据
- Android开发学习之路-回调实现Service向activity传递数据
- Android应用开发:页面跳转和数据传递
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 微信小程序开发-页面数据传递(getStorage,setStorage)
- 如何管理Isolated Storage中的数据 - [Windows Phone开发]
- Windows phone应用开发[16]-数据加密 推荐
- 面试题 - 两个页面间如何传递数据
- 在juqery的ajax和struts2整合开发时,对象数据的从页面到Action的传递问题
- windows phone:页面间传递数据
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 如何将数据从Action中传递到下一个JSP页面?
- Android开发学习之路-回调实现Service向activity传递数据
- 新手如何正确开启页面与后台数据的交互之路
- 开发MVC 2页面的时候,如何显示有换行的数据的一种方法
- 如何在小程序页面之间,传递数据和变量?