2010-10-14 232 views
0

我得到了导航起来,在我的应用程序运行,但我想躲便可欣赏到真正的路,所以我写了这个UriMapper:麻烦与UriMapper

<Application 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="MyApplication.App" 
    xmlns:navcore="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"> 
<Application.Resources> 
    <!-- Resources scoped at the Application level should be defined here. --> 
     <navcore:UriMapper x:Key="uriMapper"> 
      <navcore:UriMapping Uri="{}{page}" MappedUri="/Views/{page}.xaml" /> 
     </navcore:UriMapper> 
</Application.Resources> 
</Application> 

这里是我的导航按钮单击事件代码:

private void NavigateButton_Click(object sender, RoutedEventArgs e) 
    { 
     Button btn = sender as Button; 
     string uri = btn.Tag.ToString(); 

     this.ContentFrame.Navigate(new Uri(uri, UriKind.RelativeOrAbsolute)); 
    } 

这里是一个按钮的XAML:

<Button Content="Home" Click="NavigateButton_Click" Tag="Home" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="30" /> 

然而,当我点击一个按钮,我总体性他的错误:

Content for the URI cannot be loaded. The URI may be invalid. 

在这一行:

this.ContentFrame.Navigate(new Uri(uri, UriKind.RelativeOrAbsolute)); 

我在做什么错?

编辑:

更多XAML文件... MainPage.xaml中:

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
      mc:Ignorable="d" 
      x:Class="MyApplication.MainPage"> 

    <Grid x:Name="LayoutRoot"> 
     <Grid.Background> 
      <ImageBrush ImageSource="/bg.jpg" Stretch="Fill"/> 
     </Grid.Background> 
     <Rectangle Fill="#FF252525" Stroke="Black" Opacity="0.7" RadiusX="10" RadiusY="10" Margin="0,100,0,0" StrokeThickness="0" Width="600" HorizontalAlignment="Center" Height="350" VerticalAlignment="Top" d:LayoutOverrides="Height"/> 
     <Canvas x:Name="NavigationCanvas" Margin="0,50,0,0" HorizontalAlignment="Center" Width="600"> 
      <toolkit:ExpressionDarkTheme Height="30" Canvas.Left="188" Canvas.Top="11" Width="100" Foreground="White" Background="{x:Null}"> 
       <Button Content="Home" Click="NavigateButton_Click" Tag="Home" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="30" /> 
      </toolkit:ExpressionDarkTheme> 
         <!-- etc more buttons --> 
     </Canvas> 

     <navigation:Frame x:Name="ContentFrame" Margin="15,115,15,0" Height="320" VerticalAlignment="Top" Source="Home">    
     </navigation:Frame> 
    </Grid> 
</UserControl> 

Home.xaml:

<navigation:Page x:Class="MyApplication.Views.Home" 
    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:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" 
    Title="Home"> 

    <Grid x:Name="LayoutRoot" Width="570" Height="320" > 
     <TextBlock TextWrapping="Wrap" Foreground="White"> 
      Home. 
     </TextBlock> 
    </Grid> 

</navigation:Page> 

回答

2

这概括了问题,给this一试...

+0

这就是UriMapper应该处理的事情 - 查看第一个代码片段,URI在那里是显式的。 – 2010-10-14 15:21:06

+0

你的家是在视图文件夹? – 2010-10-14 15:23:34

+0

是的。这就是我写这个导航的方式。 – 2010-10-14 15:25:08