由Laurent Bugnion创建了基于MVVM Light模板的新SL应用程序。然后,我在/ Views目录中创建了几个导航页面 - Home.xaml,TaskPlans.xaml,Tasks.xaml和Tasks.xaml。这些页面是空的 - 我只在每个页面中创建了简单的文本块。如何将Tim Heuer导航框架模板与MVVM Light结合使用
据蒂姆Heuers模板实现导航框架我修改/Views/MainPage.xaml
<UserControl x:Class="Valachy.Administration.Views.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Helpers="clr-namespace:Valachy.Administration.Helpers"
xmlns:res="clr-namespace:Valachy.Administration.Resources"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
d:DesignWidth="640" d:DesignHeight="480"
mc:Ignorable="d"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Skins/MainSkin.xaml" />
<ResourceDictionary>
<Helpers:ResourceWrapper x:Key="ResourceWrapper" />
<Helpers:NotOperatorValueConverter x:Key="NotOperatorValueConverter" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<StackPanel Orientation="Horizontal" Width="250">
<HyperlinkButton Click="NavigateButtonClick" Tag="Home" Content="Home" FontFamily="24"></HyperlinkButton>
<HyperlinkButton Click="NavigateButtonClick" Tag="/Views/Tasks.xaml" Content="Tasks" FontFamily="24"></HyperlinkButton>
<HyperlinkButton Click="NavigateButtonClick" Tag="/Views/TaskPlans.xaml" Content="Plans" FontFamily="24"></HyperlinkButton>
</StackPanel>
</StackPanel>
<navigation:Frame x:Name="MainFrame" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="20" Source="/Views/Home.xaml" />
</Grid>
</UserControl>
这里是方法处理hyperling点击:
private void NavigateButtonClick(object sender, System.Windows.RoutedEventArgs e)
{
HyperlinkButton hyperlinkButton = sender as HyperlinkButton;
if (hyperlinkButton != null)
{
string urlString = hyperlinkButton.Tag.ToString();
Uri url = new Uri(urlString,UriKind.Relative);
MainFrame.Navigate(url);
}
}
我也改变/App.xaml在#字符后面的地址栏中隐藏/Views/Home.xaml,并在MainPage.xaml的第一个超链接按钮中更改Tag属性值。
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Valachy.Administration.App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Valachy.Administration.ViewModel"
xmlns:navcore="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
mc:Ignorable="d">
<Application.Resources>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
<navcore:UriMapper x:Key="uriMapper">
<navcore:UriMapping Uri="Home" MappedUri="/Views/Home.xaml" />
<navcore:UriMapping Uri="Tasks" MappedUri="/Views/Tasks.xaml" />
<navcore:UriMapping Uri="TaskPlans" MappedUri="/Views/TaskPlans.xaml" />
</navcore:UriMapper>
</Application.Resources>
</Application>
当我运行的应用程序,并提供导航事件点击“任务”和“TaskPlans”按钮,一切工作O.K.如果我点击“Home”超链接按钮,我会在iexplore.exe中收到System Argument异常,并显示消息“无法加载URI的内容,该URI可能无效。”
当我将第一个超链接按钮的标签内容更改回“/Views/Home.xaml”时,导航工作正常。
我可以改变标记值吗或者Urimapper如何在SL5中工作?
谢谢你的任何建议,鲁道夫。
谢谢silverfighter,你的链接对我帮助很大。 – 2012-05-02 14:22:22