页面导航【WP7学习札记之七】
2012-03-07 19:54
253 查看
本节是WP7学习札记的第七篇,讲述的内容摘要主要是将页面导航的两种方式、地址别名、页面之间的数据传递(包括传递字符串、和传递对象两种方式)、回退按钮(重写Back键的事件),具体如下:
首先讲述下Windows Phone 7应用程序的页面架构,只有一个单独的PhoneApplicationFrame,包含一个或者多个PhoneApplicationPage,也包含系统托盘和应用程序栏。
接着是页面导航的两种方式,分别是xaml方式和c#方式。需要注意的是Silverlight for Windows Phone使用以页面为基础的导航模型,与Web的页面导航模型相似,每个页面都有唯一的URI,每个页面都是没有状态的。
首先是使用xaml进行导航(NavigateUri=“/Views/Music.xaml”):
接着是c#代码的方式进行导航(NavigationService.Navigate(new Uri("/Views/Music.xaml",UriKind.Relative))):
下面是别名地址导航,这个要详细说下。具体的做法是:
在App.xaml文件的xaml文件中,引入xmlns:nav="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone" 这个xaml名称空间;在<resources>中定义如下的格式:
在App.xaml.cs中的App构造函数Public App(){}中插入如下的代码:
接着在需要使用别名导航的地方使用如下的方式:
OK,搞定~
下面讲的是页面间传递数据--字符串数据。
上面这个图不是很全面,我做点补充。首先在原页面写类似下面的代码(分别是使用了别名导航和不使用别名导航的两种情况):
然后在“导航到的页面”PageLoad事件中写如下代码(和Web开发中神似~):
OK,传递字符串完毕~
在页面间传递对象数据,NavigationService默认不能传递对象。可用的方法有:①使用App类的静态属性;②使用Singleton类;③把对象分解使用Query string来传递;
我比较倾向于使用第一种方法:
解释一下:首先在项目下添加一个类;
在App类中添加如下属性:
在传递的原页面添加如下代码:
在目标页面,代码类似如下:
Ok,解释完毕~
程序可以提供控件实现回退功能,硬件Back按钮也能回退到前一个也页面(不需要编码,内置实现~)
但是,如果回退到前一个页面是不合理的行为,可以重写“Back 按钮”的时间处理,终止回退。
注意,仅是Back键~
希望对各位博友有帮助~
首先讲述下Windows Phone 7应用程序的页面架构,只有一个单独的PhoneApplicationFrame,包含一个或者多个PhoneApplicationPage,也包含系统托盘和应用程序栏。
接着是页面导航的两种方式,分别是xaml方式和c#方式。需要注意的是Silverlight for Windows Phone使用以页面为基础的导航模型,与Web的页面导航模型相似,每个页面都有唯一的URI,每个页面都是没有状态的。
首先是使用xaml进行导航(NavigateUri=“/Views/Music.xaml”):
<HyperlinkButton NavigateUri="/Views/Music.xaml" Content="音乐" Height="30" HorizontalAlignment="Left" Margin="23,74,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="200" />
接着是c#代码的方式进行导航(NavigationService.Navigate(new Uri("/Views/Music.xaml",UriKind.Relative))):
private void button1_Click(object sender, RoutedEventArgs e) { NavigationService.Navigate(new Uri("/Views/Music.xaml", UriKind.Relative)); }
下面是别名地址导航,这个要详细说下。具体的做法是:
在App.xaml文件的xaml文件中,引入xmlns:nav="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone" 这个xaml名称空间;在<resources>中定义如下的格式:
<!--Application Resources--> <Application.Resources> <nav:UriMapper x:Key="UriMapper"> <nav:UriMapping Uri="Music" MappedUri="/Views/Music.xaml"/> <nav:UriMapping Uri="Music/{music}" MappedUri="/Views/Music.xaml?Music={music}"/> </nav:UriMapper> </Application.Resources>
在App.xaml.cs中的App构造函数Public App(){}中插入如下的代码:
this.RootFrame.UriMapper = Resources["UriMapper"] as UriMapper;
接着在需要使用别名导航的地方使用如下的方式:
<HyperlinkButton Content="音乐" NavigateUri="Music" Height="30" HorizontalAlignment="Left" Margin="23,189,0,0" Name="hyperlinkButton2" VerticalAlignment="Top" Width="200" />
OK,搞定~
下面讲的是页面间传递数据--字符串数据。
上面这个图不是很全面,我做点补充。首先在原页面写类似下面的代码(分别是使用了别名导航和不使用别名导航的两种情况):
<HyperlinkButton NavigateUri="/Views/Music.xaml?Music=歌曲1" Content="歌曲1" Height="30" HorizontalAlignment="Left" Margin="49,284,0,0" Name="hyperlinkButton3" VerticalAlignment="Top" Width="131" /> <HyperlinkButton NavigateUri="Music/歌曲1" Content="歌曲1" Height="30" HorizontalAlignment="Left" Margin="231,284,0,0" Name="hyperlinkButton4" VerticalAlignment="Top" Width="131" />
然后在“导航到的页面”PageLoad事件中写如下代码(和Web开发中神似~):
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { if(NavigationContext.QueryString.Count>0) { this.textBlock1.Text = NavigationContext.QueryString["Music"]; } }
OK,传递字符串完毕~
在页面间传递对象数据,NavigationService默认不能传递对象。可用的方法有:①使用App类的静态属性;②使用Singleton类;③把对象分解使用Query string来传递;
我比较倾向于使用第一种方法:
解释一下:首先在项目下添加一个类;
public class MusicClass { public string Name { get; set; } public string File { get; set; } }
在App类中添加如下属性:
public static MusicClass Music { get; set; }//添加这个属性
在传递的原页面添加如下代码:
private void button2_Click(object sender, RoutedEventArgs e) { App.Music = new MusicClass() { Name="歌曲1", File="music1.mp3" }; NavigationService.Navigate(new Uri("/Views/Music.xaml", UriKind.Relative)); }
在目标页面,代码类似如下:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { if (App.Music != null) { this.textBlock1.Text = App.Music.Name; } }
Ok,解释完毕~
程序可以提供控件实现回退功能,硬件Back按钮也能回退到前一个也页面(不需要编码,内置实现~)
但是,如果回退到前一个页面是不合理的行为,可以重写“Back 按钮”的时间处理,终止回退。
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; }
注意,仅是Back键~
希望对各位博友有帮助~
相关文章推荐
- WP7.页面导航
- WP7 页面导航报错 跳到System.Diagnostics.Debugger.Break
- WP7页面导航概述
- Panorama控件和Pivot控件【WP7学习札记之十四】
- 学习札记——JSP实现登陆后返回历史浏览页面
- 解决vs2010 utimate中文版添加Silverlight for WP7模板方案【WP7学习札记之一】
- windows phone7 学习笔记02——页面导航
- windows phone 学习之页面导航和数据传递
- 反应性扩展框架(Reactive Extensions)【WP7学习札记之十六】
- ASP.NET学习笔记2----用站点导航控件与母版页搭建页面框架
- WP7->界面->页面导航、切换及参数传递
- wp7 页面导航传值和在Tombstoning之后保存状态保存状态
- 【转载】wp7中从墓碑化中重新激活时如何从app.xaml.cs中直接导航到程序的某个页面
- 启动器与选择器-常用Task【WP7学习札记之四】
- Windows Phone 7开发学习(二)--页面导航
- android IM应用学习之导航页面viewpager的学习(一)
- 屏幕方向与常用控件【WP7学习札记之六】
- 启动器与选择器-运行模式【WP7学习札记之十二】
- 设备的开发【WP7学习札记之五】
- WP7页面导航