博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

windowsPhone总结_页面导航

Posted on 2011-03-07 10:19  星尘的天空  阅读(285)  评论(0编辑  收藏  举报

windowsphone页面导航方法总结

用于Windows Phone 的SilverLight 提供了 PhoneApplicationFrame 和PhoneApplicationPage类,这两个类是微软针对Silverlight for Windows Phone另外封装的,它为导航提供了使得。
PhoneApplicationPage 控件代表了内容相互分离的区段,一个应用程序可以有多个PhoneApplicationPage 。
PhoneApplicationFrame扮演了页面控件容器的角色,对页面之间的导航提供了便利,一个应用程序有一个独立的PhoneApplicationFrame。
Windows Phone 7是通过 使用URI【通用资源标志符(Uniform Resouce Identifier)】映射进行页面导航的。

1)第一种通过使用NavigationService 进行导航控制,如下代码:

NavigationService.Navigate(new Uri("/Layout/SecondPage.xaml", UriKind.Relative));

UriKind 是一个枚举类型,自带有三种类型,这里我们使用相对路径。

这里我们选择的是相路径,那么我就要注意URL的书写方法,注意前面要添加这个符号“/”,她表示相对路径的标志

2)第二种通过直接使用控件自带功能,即使用微软的HyperLink控件,直接导航,代码如下:

<HyperlinkButton Content="点转" NavigateUri="/Layout/SecondPage.xaml" Height="30" HorizontalAlignment="Left" Margin="101,68,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="200" />
3)第三种我们可以为我们的URI路径起一个别名,然后在调用时调用别名即可,可以方便我们管理,使用步骤如下:

a).在App.xaml的文件上注册Windows.Navigation命名空间,该空间位于Reference目录下的Microsoft.Phone组件,代码如下:

xmlns:nav="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone"

b).为应用程序添加资源,代码如下:

<Application.Resources>
<nav:UriMapper x:Key="UriMapper">
<nav:UriMapping Uri="SecondPage" MappedUri="/Layout/SecondPage.xaml"/>

</nav:UriMapper>
</Application.Resources>

c).添加完成后,进入 App.xaml.cs 在它的构造函数里面为应用程序的RootFrame下所在的UriMapper赋值,这用的话我们应用程序就可以通过它去映射我们传给它的URI 别名。代码如下:

this.RootFrame.UriMapper = Resources["UriMapper"] as UriMapper;

Tip:这里的Resources 是一个字典型数据,键值必须跟上面App.xaml 添加资源的UriMapper Key要相对应。

d).注册好别名之后,我们就可以在程序中如下使用:

NavigationService.Navigate(new Uri("SecondPage",UriKind.Relative));

导航到多个页面的情况:
App.xaml //前台代码

 <nav:UriMapper  x:Key="uriMapper">
            <nav:UriMapping Uri="Page1"  MappedUri="/Page1.xaml"></nav:UriMapping>//第一个映射页面
            <nav:UriMapping Uri="Page2" MappedUri="/page/Page2.xaml"></nav:UriMapping>//第二个映射页面
  </nav:UriMapper>

App.xaml.cs//后台代码

this.RootFrame.UriMapper = Resources["uriMapper"] as UriMapper;//在构造函数里面调用

按钮单机调用事件

NavigationService.Navigate(new Uri("Page1", UriKind.Relative));

NavigationService.Navigate(new Uri("Page2", UriKind.Relative));

或者在HyperLinkButton 中如下使用:

<HyperlinkButton Content="点转" NavigateUri="SecondPage" Height="30" HorizontalAlignment="Left" Margin="101,68,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="200" />