Windows phone 7之页面导航
2012-04-06 21:57
393 查看
首先说一下WP7程序的呈现主体与关系,直接呈现给用户的界面是页面Page,每个Page是继承自PhoneApplicationPage的类,一个程序可以包含任意多个的Page页面,这些Page页面被放在一个共同的Frame下,Frame继承自PhoneApplicationFrame 。一个应用程序只能有一个Frame,创建程序时自动生成的App.xaml.cs文件中,有关于程序Frame初始化的代码,在InitializePhoneApplication()方法中,如下
可以看到,注释提到,不要添加对于的代码到该方法中,所以我们也不要这么做。
当我们改变某个Page的属性时不会影响其他页面,但是一旦改变了Frame,那就会影响所有的页面,所以,我们应该认为Frame是只读的(虽然不是)。
App类是程序主类,我没刻意用它来操作全局的属性,比如获取Frame,就是RootFrame属性。
页面导航的方法
页面导航一般分为两种方法,代码导航,和XAML声明导航
1、代码实现
NavigationService类提供了导航的功能,NavigationService.Navigate(new Uri("/NewPage.xaml", UriKind.Relative));将这段代码放到Button点击事件中,点击Button就会跳转到NewPage.xaml页面,其中naviagte接受一个Uri类型的参数,这里传入字符串路径和路径类型UriKind,UriKind是个枚举型,一边页面导航是相对路径。WP7的特点的从根目录起,"/"开头表示根目录,依次输入文件路径,NewPage.xaml文件放在了根目录下,所以路径写为"/NewPage.xaml", 如果NewPage.xaml在根目录的View文件夹下,则路径为"/View/NewPage.xaml"。
2、XAML实现
可以利用导航控件如 HyperlinkButton,写法<HyperlinkButton NavigateUri="/NewPage.xaml" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />,NavigateUri属性用来设置路径。
上述两种方式达到的目的是一样的,点击Button或HyperlinkButton就会跳转到NewPage.xaml
WP7页面导航可以使用路径别名,也是从Silverlight继承过来的
首先在App.xaml的文件上注册Windows.Navigation 命名空间,代码:mlns:navigate="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone",
然后注册资源代码如下:
最后给Frame添加UriMapper :this.RootFrame.UriMapper = Resources["UriMapper"] as UriMapper; 将这句代码写到App类的构造函数里即可。
使用方式
NavigationService.Navigate(new Uri("NewPage", UriKind.Relative));
<HyperlinkButton NavigateUri="NewPage" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />
路径中直接填别名就可以了,运行效果还是一样的。
页面之间传递参数
页面传递参数和网页中传递参数的方式一样,在路径的后面加上"?参数名=参数值"
NavigationService.Navigate(new Uri("/NewPage.xaml?ID=10", UriKind.Relative));
<HyperlinkButton NavigateUri="/NewPage.xaml?ID=10" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />
这样就导航到了NewPage.xaml并将ID及其值传递到了NewPage.xaml中。
在NewPage.xaml中接受参数的方法
NavigationContext的QueryString属性存储了上个页面中传递的所有参数下面代码获取ID并用MessageBox显示其值
导航中使用了别名的话也可以将参数传递变得简单,修改资源为
这样通过
NavigationService.Navigate(new Uri("NewPage/10", UriKind.Relative));
<HyperlinkButton NavigateUri="NewPage/10" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />
就可以将ID=10传递到NewPage.xaml,
由于定义了两个导航到UriMapping,表现方式不一样,参数传递方世也不一样,加上默认不使用别名的方式,给Newpage.xaml传递参数,路径可以有三种写法
"/NewPage.xaml?ID=10“,"NewPage?ID=10“,"NewPage/ID=10“,效果一样。
页面传递参数是页面之间共享数据的一种方式,还有两种比较创建的数据共享方法,一种是全局变量,比如使用静态类,和APP类的静态成员,这种方法比较简单,会C#的童鞋都会使用,还有一种方式是使用IsolatedStorage类,这也是集成自Silverlight,叫做独立存储。独立存储在WP7程序的声明周期和数据共享起到最重要的作用,没有之一,所以,无论如何也要掌握,我将之留到介绍WP7程序的声明周期时详细讲。这里只写一下他最简单用法,不理解的童鞋别急。
在MainPage.xaml.cs中写入
在NewPage.xaml.cs中写入
最终运行和NavigationService跳转在路径后面加参数效果是一样的
我新浪微博的昵称是"@马蔬菜",多多关注,谢谢
// Do not add any additional code to this method private void InitializePhoneApplication() { if (phoneApplicationInitialized) return; // Create the frame but don't set it as RootVisual yet; this allows the splash // screen to remain active until the application is ready to render. RootFrame = new PhoneApplicationFrame(); RootFrame.Navigated += CompleteInitializePhoneApplication; // Handle navigation failures RootFrame.NavigationFailed += RootFrame_NavigationFailed; // Ensure we don't initialize again phoneApplicationInitialized = true; }
可以看到,注释提到,不要添加对于的代码到该方法中,所以我们也不要这么做。
当我们改变某个Page的属性时不会影响其他页面,但是一旦改变了Frame,那就会影响所有的页面,所以,我们应该认为Frame是只读的(虽然不是)。
App类是程序主类,我没刻意用它来操作全局的属性,比如获取Frame,就是RootFrame属性。
页面导航的方法
页面导航一般分为两种方法,代码导航,和XAML声明导航
1、代码实现
NavigationService类提供了导航的功能,NavigationService.Navigate(new Uri("/NewPage.xaml", UriKind.Relative));将这段代码放到Button点击事件中,点击Button就会跳转到NewPage.xaml页面,其中naviagte接受一个Uri类型的参数,这里传入字符串路径和路径类型UriKind,UriKind是个枚举型,一边页面导航是相对路径。WP7的特点的从根目录起,"/"开头表示根目录,依次输入文件路径,NewPage.xaml文件放在了根目录下,所以路径写为"/NewPage.xaml", 如果NewPage.xaml在根目录的View文件夹下,则路径为"/View/NewPage.xaml"。
2、XAML实现
可以利用导航控件如 HyperlinkButton,写法<HyperlinkButton NavigateUri="/NewPage.xaml" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />,NavigateUri属性用来设置路径。
上述两种方式达到的目的是一样的,点击Button或HyperlinkButton就会跳转到NewPage.xaml
WP7页面导航可以使用路径别名,也是从Silverlight继承过来的
首先在App.xaml的文件上注册Windows.Navigation 命名空间,代码:mlns:navigate="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone",
然后注册资源代码如下:
<Application.Resources> <navigate:UriMapper x:Key="UriMapper"> <navigate:UriMapping Uri="NewPage" MappedUri="/NewPage.xaml"/> </navigate:UriMapper> </Application.Resources>
最后给Frame添加UriMapper :this.RootFrame.UriMapper = Resources["UriMapper"] as UriMapper; 将这句代码写到App类的构造函数里即可。
使用方式
NavigationService.Navigate(new Uri("NewPage", UriKind.Relative));
<HyperlinkButton NavigateUri="NewPage" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />
路径中直接填别名就可以了,运行效果还是一样的。
页面之间传递参数
页面传递参数和网页中传递参数的方式一样,在路径的后面加上"?参数名=参数值"
NavigationService.Navigate(new Uri("/NewPage.xaml?ID=10", UriKind.Relative));
<HyperlinkButton NavigateUri="/NewPage.xaml?ID=10" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />
这样就导航到了NewPage.xaml并将ID及其值传递到了NewPage.xaml中。
在NewPage.xaml中接受参数的方法
NavigationContext的QueryString属性存储了上个页面中传递的所有参数下面代码获取ID并用MessageBox显示其值
if (NavigationContext.QueryString.ContainsKey("ID")) { MessageBox.Show(NavigationContext.QueryString["ID"]); }
导航中使用了别名的话也可以将参数传递变得简单,修改资源为
<Application.Resources> <navigate:UriMapper x:Key="UriMapper"> <navigate:UriMapping Uri="NewPage" MappedUri="/NewPage.xaml"/> <navigate:UriMapping Uri="NewPage/{param}" MappedUri="/NewPage.xaml?ID={param}"/> </navigate:UriMapper> </Application.Resources>
这样通过
NavigationService.Navigate(new Uri("NewPage/10", UriKind.Relative));
<HyperlinkButton NavigateUri="NewPage/10" Content="goto new page" Height="30" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="200" />
就可以将ID=10传递到NewPage.xaml,
由于定义了两个导航到UriMapping,表现方式不一样,参数传递方世也不一样,加上默认不使用别名的方式,给Newpage.xaml传递参数,路径可以有三种写法
"/NewPage.xaml?ID=10“,"NewPage?ID=10“,"NewPage/ID=10“,效果一样。
页面传递参数是页面之间共享数据的一种方式,还有两种比较创建的数据共享方法,一种是全局变量,比如使用静态类,和APP类的静态成员,这种方法比较简单,会C#的童鞋都会使用,还有一种方式是使用IsolatedStorage类,这也是集成自Silverlight,叫做独立存储。独立存储在WP7程序的声明周期和数据共享起到最重要的作用,没有之一,所以,无论如何也要掌握,我将之留到介绍WP7程序的声明周期时详细讲。这里只写一下他最简单用法,不理解的童鞋别急。
在MainPage.xaml.cs中写入
private void btnNavigate_Click(object sender, RoutedEventArgs e) { IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings; iss["ID"] = 10; NavigationService.Navigate(new Uri("NewPage", UriKind.Relative)); }
在NewPage.xaml.cs中写入
IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings; if (iss.Contains("ID")) { MessageBox.Show(iss["ID"].ToString()); }
最终运行和NavigationService跳转在路径后面加参数效果是一样的
我新浪微博的昵称是"@马蔬菜",多多关注,谢谢
相关文章推荐
- Windows Phone笔记(7)页面间导航以及数据传递
- Windows Phone 7 开发探索笔记5——页面间导航
- 与众不同 windows phone (27) - Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏
- Windows Phone在导航中移除页面进行跳转
- Windows Phone 7 页面导航以及异常处理
- Windows Phone 8.1基础教程(1) 页面导航、弹出框
- Windows Phone页面导航和独立存储开发总结
- windows phone页面的简单导航操作
- Windows Phone7天初学(2):页面导航
- Windows Phone页面导航和独立存储开发总结
- Windows phone开发初体验之(二)-页面导航
- Windows Phone 页面导航动画
- Windows phone开发初体验之-页面导航
- Windows Phone 7 开发 31 日谈——第2日:页面导航
- [Windows Phone 7]如何导航页面和页面间传值
- windows phone 学习之页面导航和数据传递
- Windows Phone 系列- Simple MVVM Navigation 页面导航的实现
- Windows Phone 8初学者开发—第18部分:在页面间导航
- Windows Phone 7开发——页面间导航【转】
- windows phone:在MVVM架构下进行页面导航