Windows Phone开发教程7.docx
- 文档编号:4153067
- 上传时间:2022-11-28
- 格式:DOCX
- 页数:12
- 大小:479.31KB
Windows Phone开发教程7.docx
《Windows Phone开发教程7.docx》由会员分享,可在线阅读,更多相关《Windows Phone开发教程7.docx(12页珍藏版)》请在冰豆网上搜索。
WindowsPhone开发教程7
WindowsPhone笔记(7)页面间导航以及数据传递
WindowsPhone笔记之前的示例都只是基于单个页面的简单示例,一般是继承了PhoneApplicationPage类的MainPage页面,但是实际中的应用程序却不可能这么简单,肯定都是由多个页面组成的,那么这就要求我们首先要了解:
WindowsPhone的页面之间是如何跳转(导航)的?
以及如何在页面间传值?
这就是这篇笔记需要解决的问题。
1.页面间的导航
WindowsPhone中页面间的导航非常简单,有过B/S开发经验的开发人员会发现WindowsPhone页面间的导航基本上和HTML页面的导航一样。
现在我们通过一个简单的示例程序来了解在WindowsPhone中如何进行页面间的跳转(导航)。
首先创建一个SilverlightforWindowsPhone项目,MainPage.xaml代码如下:
1
--ContentPanel-在此处放置其他内容-->
2 Name="ContentPanel"Grid.Row="1"Margin="12,0,12,0"> 3 4 复制代码 后台处理MainPage.xaml.cs页面的代码: 1publicpartialclassMainPage: PhoneApplicationPage 2{ 3Randomran=newRandom(); 4//构造函数 5publicMainPage() 6{ 7InitializeComponent(); 8} 9 10/// 11 ///触摸TextBlock以外的屏幕时发生 12 /// 13 /// 14protectedoverridevoidOnManipulationStarted(ManipulationStartedEventArgse) 15{ 16ContentPanel.Background=newSolidColorBrush(Color.FromArgb(255,(byte)ran.Next(255),(byte)ran.Next(255),(byte)ran.Next(255))); 17base.OnManipulationStarted(e); 18} 19 20/// 21 ///触摸TextBlock控件跳转到SecondPage 22 /// 23 /// 24 /// 25privatevoidtblNavigationToSecondPage_ManipulationStarted(objectsender,ManipulationStartedEventArgse) 26{ 27this.NavigationService.Navigate(newUri("/SecondPage.xaml",UriKind.Relative)); 28 29e.Complete(); 30e.Handled=true; 31} 32} 复制代码 完成MainPage页面代码编写后,新建一个SecondPage页面 SecondPage.xaml代码: 1 --ContentPanel-在此处放置其他内容--> 2 Name="ContentPanel"Grid.Row="1"Margin="12,0,12,0"> 3 4 复制代码 后台处理SecondPage.xaml.cs代码: 1publicpartialclassSecond: PhoneApplicationPage 2{ 3Randomran=newRandom(); 4publicSecond() 5{ 6InitializeComponent(); 7} 8 9privatevoidtblNavigationToMainPage_ManipulationStarted(objectsender,ManipulationStartedEventArgse) 10{ 11this.NavigationService.Navigate(newUri("/MainPage.xaml",UriKind.Relative)); 12//this.NavigationService.GoBack(); 13e.Complete(); 14e.Handled=true; 15} 16} 复制代码 以上是该示例的全部代码,两个页面的结构类似,点击TextBlock控件以外的屏幕区域就会改变页面背景颜色,点击MainPage页面的TextBlock控件会跳转到SecondPage页面,如果点击SecondPage页面的话就会调用页面的GoBack()方法,那么现在我们通过运行程序来分析页面导航的一些技术要点。 首先点击MainPage页面,改变页面的背景色,然后点击TextBlock控件跳转到SecondPage页面,点击SecondPage页面的TextBlock控件退回MainPage页面: 下面我们做一个简单的修改,把SecondPage页面的TextBlock元素触摸事件的处理代码改为: 1privatevoidtblNavigationToMainPage_ManipulationStarted(objectsender,ManipulationStartedEventArgse) 2{ 3this.NavigationService.Navigate(newUri("/MainPage.xaml",UriKind.Relative)); 4//this.NavigationService.GoBack(); 5e.Complete(); 6e.Handled=true; 7} 复制代码 再次和刚才一样的操作,运行效果如下: SilverlightforWindowsPhone的导航系统是基于栈(stack)的,stack是一种后进先出的数据结构。 在这里我们把调用Navigate()方法的页面成为源(source)页面,把导航到的页面成为目标(destination)页面。 在第一个示例中,源页面MainPage页面调用Navigation方法时,该页面被放进栈中,同时创建目标页面SecondPage的一个新的实例并显示出来,当调用目标页面的GoBack()方法时(相当于手机中的Back键,在程序初始状态下按该键应用程序会被终止),SecondPage页面的实例会被抛弃,位于栈顶部的MainPage实例就会弹出并显示出来。 所有我们可以看到MainPage页面的背景色还是之前的颜色。 第二个示例中点击SecondPage页面的TextBlock控件我们可以看到页面的颜色已经回到初始状态,这就表示: SecondPage导航到的是一个MainPage页面的一个新的实例,我们要记住这点: 调用源页面的Navigation方法会创建一个目标页面的新的实例,之前的实例会被丢弃。 2.页面间数据的传递 和前面的导航类似,WindowsPhone页面间的数据传递也和传统的HTML页面通过URI传递数据的方式相似。 下面我们通过一个示例程序来演示在WindowsPhone程序中页面如何进行数据的传递。 MainPage.xaml代码: 1 --ContentPanel-在此处放置其他内容--> 2 Name="ContentPanel"Grid.Row="1"Margin="12,0,12,0"> 3 4 复制代码 MainPage.xaml.cs后台处理程序: 1publicpartialclassMainPage: PhoneApplicationPage 2{ 3Randomran=newRandom(); 4//构造函数 5publicMainPage() 6{ 7InitializeComponent(); 8} 9 10/// 11 ///触摸TextBlock控件时触发 12 /// 13 /// 14 /// 15privatevoidtblNavigationSecondPage_ManipulationStarted(objectsender,ManipulationStartedEventArgse) 16{ 17Stringdestination="/SecondPage.xaml"; 18 19//当ContentPanel的背景色不是默认的值(Brush类型null)时 20 //将背景颜色传递到目标页面中去 21if(this.ContentPanel.BackgroundisSolidColorBrush) 22{ 23Colorclr=(ContentPanel.BackgroundasSolidColorBrush).Color; 24destination+=string.Format("? Red={0}&Green={1}&Blue={2}",clr.R,clr.G,clr.B); 25} 26this.NavigationService.Navigate(newUri(destination,UriKind.Relative)); 27 28e.Complete(); 29e.Handled=true; 30} 31 32/// 33 ///触摸TextBlock控件以外的屏幕改变背景色 34 /// 35 /// 36protectedoverridevoidOnManipulationStarted(ManipulationStartedEventArgse) 37{ 38this.ContentPanel.Background=newSolidColorBrush(Color.FromArgb(255,(byte)ran.Next(255),(byte)ran.Next(255),(byte)ran.Next(255))); 39base.OnManipulationStarted(e); 40} 41} 复制代码 接着,同样也新建一个SecondPage页面。 SecondPage.xaml代码如下: 1 --ContentPanel-在此处放置其他内容--> 2 Name="ContentPanel"Grid.Row="1"Margin="12,0,12,0"> 3 4 复制代码 SecondPage.xaml.cs后台处理程序: 1protectedoverridevoidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgse) 2{ 3//通过NavigationContext对象获取源页面传递的字符串,并存放在字典对象中 4IDictionary 5//传递的字符串中有Red的话 6if(parameters.ContainsKey("Red")) 7{ 8byteR=Byte.Parse(parameters["Red"]); 9byteG=Byte.Parse(parameters["Green"]); 10byteB=Byte.Parse(parameters["Blue"]); 11//改变背景色 12this.ContentPanel.Background=newSolidColorBrush(Color.FromArgb(255,R,G,B)); 13} 14 15base.OnNavigatedTo(e); 16} 17 18privatevoidtblGoBack_ManipulationStarted(objectsender,ManipulationStartedEventArgse) 19{ 20this.NavigationService.GoBack(); 21 22e.Complete(); 23e.Handled=true; 24} 复制代码 编译运行,首先改变MainPage页面背景色,然后点击MainPage页面的TextBlock控件导航到SecondPage页面,可以看到源页面的背景色值被传递到目标页面中,并显示出来。 分析代码: 在SecondPage页面的后台处理程序中,我们重写了定义在Page类中的OnNavigation方法,这个方法在页面执行完初始化的构造函数后马上调用;我们通过页面的NavigationContext属性来访问源页面传递的字符串,该对象只有一个属性: QueryString,这个属性返回的是一个字典容器。 参考资料: 《ProgrammingWindowsPhone7MicrosoftSilverlightEdition》 作者: 晴天猪 出处: 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Windows Phone开发教程7 Phone 开发 教程
![提示](https://static.bdocx.com/images/bang_tan.gif)