您的位置:首页 > 其它

页面导航【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”):



<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键~
希望对各位博友有帮助~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: