0

由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中工作?

謝謝你的任何建議,魯道夫。

回答

1

退房Laurent的談話在混合「深潛MVVM」

在這次談話,他談到如何與MVVM導航

。他認爲導航服務... 這次演講是真正爲中晚期原發性肝癌的一些技術MVVM一個很好的手錶......

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

+0

謝謝silverfighter,你的鏈接對我幫助很大。 – 2012-05-02 14:22:22