推薦看Tim Heuer的視頻:http://silverlight.net/learn/videos/silverlight-videos/navigation-framework/
建立SL應用程式後,首先引入System.Windows.Controls和System.Windows.Controls.Navigation。
在App中引用命名空間:
xmlns:navcore="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
添加映射規則:
<navcore:UriMapper x:Key="uriMapper" >
<navcore:UriMapping Uri="Home" MappedUri="/Views/HomePage.xaml" />
</navcore:UriMapper>
然後在MainPage中引用命名空間:
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
添加導覽按鈕:
<HyperlinkButton Tag="Home" Click="NavigateButton_Click" Content="Home" FontSize="24" />
並建立事件:
private void NavigateButton_Click(object sender, RoutedEventArgs e)
{
HyperlinkButton btn = sender as HyperlinkButton;
string url = btn.Tag.ToString();
this.MainFrame.Navigate(new Uri(url, UriKind.Relative));
}
添加導航架構:
<navigation:Frame x:Name="MainFrame" UriMapper="{StaticResource uriMapper}" HorizontalAlignment="Stretch" Margin="20" Source="Home" />
PS:Tim Heuer那裡下載的代碼不能運行,必須添加 UriMapper="{StaticResource uriMapper}" .這個問題困擾了我好久,還好視頻裡面的評論有人提到,我才解決。
在SL項目中添加檔案夾View,然後添加HomePage.xaml。