我們可以使用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;
}
這時按實體回退按鈕不會回退;