Fork me on GitHub

SL学习笔记 -- Navigation Framework

   推荐看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。

posted @ 2010-03-16 14:19  idoku  阅读(344)  评论(0编辑  收藏  举报