我们可以使用HypelinkButton导航页面,假如我们已经有了Page1,Page2,Page3,MainPage几个页面:
通过HypelinkButton的NavigateUri属性输入Uri地址:
- < HyperlinkButton Content ="Page1" NavigateUri ="/Page1.xaml" Height ="30" HorizontalAlignment ="Left" Margin ="10,10,0,0" Name ="hyperlinkButton1" VerticalAlignment ="Top" Width ="200" />
- < HyperlinkButton Content ="Page2 NavigateUri ="/Page2.xaml" Height ="30" HorizontalAlignment ="Left" Margin ="40,10,0,0" Name ="hyperlinkButton2" VerticalAlignment ="Top" Width ="200" />
- < HyperlinkButton Content ="Page3" NavigateUri = "/Page3.xaml" Height ="30" HorizontalAlignment ="Left" Margin ="70,10,0,0" Name ="hyperlinkButton3" VerticalAlignment ="Top" Width ="200" />
还可以通过按钮假如我们有三个按钮可以共享一个Click事件
- < Button x : Name ="Page1Button" Content ="Page1" Click ="Button_Click" Width ="200" Height ="75" />
- < Button x : Name ="Page2Button" Content ="Page2" Click ="Button_Click" Width ="200" Height ="75" />
- < Button x : Name ="Page3Button" Content ="Page3" Click ="Button_Click" Width ="200" Height ="75" />
然后我们在C#中定义Click事件代码
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- Button clickedButton = (Button)sender ;
- switch (clickedButton.Name)
- {
- case "PastaButton":
- NavigationService.Navigate(new Uri("/Pasta.xaml", UriKind.Relative));
- break;
- case "SauceButton":
- NavigationService.Navigate(new Uri("/Sauce.xaml", UriKind.Relative));
- break;
- case "CheeseButton":
- NavigationService.Navigate(new Uri("/Cheese.xaml", UriKind.Relative));
- break;
- }
- }
按别名导航
首先在App.xaml的Application中添加命名空间
xmlns:nav="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone"
然后在在Application的Application.resources中
<Application.Resources>
<nav:UriMapper x:Key="UriMapper">
<nav:UriMapping Uri="Music" MappedUri="/Views/Music.xaml"/>
</nav:UriMapper>
</Application.Resources>
<Application.Resources>
<nav:UriMapper x:Key="UriMapper">
<nav:UriMapping Uri="Music/{song}" MappedUri="/Views/Music.xaml?Song={song}"/>
</nav:UriMapper>
</Application.Resources>
private void button1_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("Music/第一首歌曲", UriKind.Relative));
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
this.textBlock1.Text = "准备播放" + NavigationContext.QueryString["Song"];
}
以上代码是将关键字“Music”映射到“/views/Music.xaml”路径。
然后在public App()中写入
this.RootFrame.UriMapper = Resources["UriMapper"] as UriMapper;
之后就可以在控件Uri中直接输入关机字即可以导航到映射地址。
C#代码是在App.xaml.cs中定义
public App()
{
var mapper = new UriMapper();
mapper.UriMappings.Add(CreateUriMapping("Music","/Music.xaml"));
RootFrame.UriMapper = mapper;
}
private UriMapping CreateUriMapping(string uriAsString, string mappedUriAsString)
{
return new UriMapping()
{
Uri = new Uri(uriAsString, UriKind.Relative),
MappedUri = new Uri(mappedUriAsString, UriKind.Relative)
};
}
在程序中使用 this.NavigationService.Navigate(new Uri("music",UriKind.Relative)); 即可导航到该页面。
如果程序不希望用户使用回退按钮,则可以在ApplicationPage添加BackKeyPress="PhoneApplicationPage_BackKeyPress属性
然后重写
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
这时按物理回退按钮不会回退;