如何将Tim Heuer导航框架模板与MVVM Light相结合

时间:2012-02-27 14:32:00

标签: navigation mvvm-light silverlight-5.0 navigation-framework

我根据Laurent Bugnion的MVVM Light模板创建了新的SL应用程序。然后我在/ Views目录中创建了几个导航页面 - Home.xaml,TaskPlans.xaml,Tasks.xaml和Tasks.xaml。那些页面是空的 - 我在每个页面中只创建了简单的文本块。

根据Tim 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.如果我单击“主页”超链接按钮,我会在iexplore.exe中收到System Argument异常,并显示消息“无法加载URI的内容。URI可能无效。”

当我将第一个超链接按钮的标签内容更改回“/Views/Home.xaml”时,导航工作正常。

我可以以某种方式更改标记值,或者Urimapper在SL 5中的工作方式有所不同吗?

感谢任何建议,鲁道夫。

1 个答案:

答案 0 :(得分:1)

查看Laurent在Mix“深潜MVVM”中的演讲

在这次演讲中,他谈到了如何使用MVVM进行导航。他建议导航服务...... 对于一些先进的MVVM技术来说,这次演讲真的很棒......

http://channel9.msdn.com/Events/MIX/MIX11/OPN03