2011-03-07 131 views
0

Silverlight中的很多东西似乎都很简单......这就是其中之一。我有下面的MainPage xaml和Home.xaml的代码示例。对Silverlight导航感到困惑

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

<Grid x:Name="LayoutRoot" Style="{StaticResource LayoutRootGridStyle}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto" MinHeight="202"/> 
    </Grid.RowDefinitions> 

    <Border Grid.RowSpan="3" Background="{StaticResource NavPageLinedBrush}" /> 

    <Border x:Name="BrandingBorder" Style="{StaticResource NavBrandingBorderStyle}"> 
     <StackPanel x:Name="BrandingStackPanel" Style="{StaticResource NavBrandingStackPanelStyle}" > 
      <ContentControl Style="{StaticResource NavLogoIcon}" Content="Company " /> 
      <TextBlock x:Name="ApplicationNameTextBlock" Style="{StaticResource ApplicationNameStyle}" Text="Sample System" /> 
     </StackPanel> 
    </Border> 

    <Border x:Name="LinksBorder" Style="{StaticResource NavLinksBorderStyle}" Grid.Row="1"> 
     <StackPanel x:Name="LinksStackPanel" Style="{StaticResource LinksStackPanelStyle}"> 
      <HyperlinkButton Style="{StaticResource LinkStyle}" NavigateUri="Home" TargetName="ContentFrame" Content="home" /> 
      <HyperlinkButton Style="{StaticResource LinkStyle}" NavigateUri="About" TargetName="ContentFrame" Content="about" /> 
     </StackPanel> 
    </Border> 

    <Border x:Name="ContentBorder" Style="{StaticResource NavContentBorderStyle}" Grid.Row="2"> 
     <navigation:Frame x:Name="ContentFrame" Style="{StaticResource NavContentFrameStyle}" Navigated="ContentFrame_Navigated" NavigationFailed="ContentFrame_NavigationFailed" /> 
    </Border> 
</Grid> 

<navigation:Page x:Class="RMS.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:sys="clr-namespace:System;assembly=mscorlib" 
xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" 
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
d:DesignWidth="640" mc:Ignorable="d" 
Style="{StaticResource PageStyle}" Height="423" Width="768"> 

<Grid x:Name="LayoutRoot" Width="768"> 
    <Canvas Margin="0,0,8,8" Width="768"> 
     <TextBlock x:Name="usernameLbl" Canvas.Left="231" TextWrapping="Wrap" Text="Username:" Canvas.Top="185"/> 
     <TextBlock x:Name="passwordLbl" Canvas.Left="231" TextWrapping="Wrap" Text="Password:" Canvas.Top="227"/> 
     <TextBox x:Name="usernameTxt" Canvas.Left="333" TextWrapping="Wrap" Canvas.Top="180" Width="197"/> 
     <TextBox x:Name="passwordTxt" Canvas.Left="333" TextWrapping="Wrap" Canvas.Top="225" Width="197"/> 
     <Image Height="53" Source="/RMS;component/gw_pro_res_CGS.jpg" Stretch="Fill" Canvas.Left="114" Canvas.Top="57"/> 
     <Button Content="Login" Canvas.Left="455" Canvas.Top="275" Width="75" Click="Button_Click" /> 
     <controlsToolkit:BusyIndicator x:Name="Busy" Content="" Canvas.Left="318" Canvas.Top="171" Height="88" Width="228" RenderTransformOrigin="0.496,-0.184" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed"/> 
    </Canvas> 
</Grid> 

经过一定方法Home.xaml.cs执行上我称之为:this.NavigationService.Navigate(new uri("/Views/About.xaml"));

然而,出现了错误,并表示该页面无法找到。该URI看起来是正确的。还有什么可能是错的?

编辑:App.xaml中

<Application 
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" 
x:Class="RMS.App" 
mc:Ignorable="d"> 

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Assets/Styles.xaml"/> 
      <ResourceDictionary Source="Assets/CoreStyles.xaml"/> 
      <ResourceDictionary Source="Assets/SDKStyles.xaml"/> 
      <!--<ResourceDictionary Source="Assets/ToolkitStyles.xaml"/> 
      To extend this theme to include the toolkit controls: 
      1. Install the Silverlight Toolkit for Silverlight 4 
      2. Add a Toolkit control to your project from the toolbox. This will add references to toolkit assemblies. 
      3. Change the "Build Action" for ToolkitStyles.xaml to "Page" 
      4. Uncomment the resource dictionary include above. 

      If you do not intend to use toolkit controls, delete this comment and the ToolkitStyles.xaml file.--> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

NavContent:

<!-- **STYLE UPDATES FOR NAV TEMPLATE** --> 
<!-- ********************************** --> 
<Style x:Key="NavContentBorderStyle" TargetType="Border" BasedOn="{StaticResource ContentBorderStyle}"> 
    <Setter Property="Margin" Value="0" /> 
    <Setter Property="Background" Value="Transparent" /> 
</Style> 

<Style x:Key="NavContentFrameStyle" TargetType="navigation:Frame" BasedOn="{StaticResource ContentFrameStyle}"> 
    <Setter Property="Margin" Value="34,0,34,34"/> 
    <Setter Property="UriMapper"> 
     <Setter.Value> 
      <uriMapper:UriMapper> 
       <uriMapper:UriMapping MappedUri="/Views/Home.xaml" Uri="" /> 
       <uriMapper:UriMapping MappedUri="/Views/{pageName}.xaml" Uri="/{pageName}" /> 
       <uriMapper:UriMapping MappedUri="/Views/{pageName}.xaml" Uri="{}{pageName}" /> 
      </uriMapper:UriMapper> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="NavBrandingBorderStyle" TargetType="Border" BasedOn="{StaticResource BrandingBorderStyle}"> 
    <Setter Property="Margin" Value="0,15,36,0"/> 
    <Setter Property="VerticalAlignment" Value="Top"/> 
    <Setter Property="Padding" Value="0,5,0,0"/> 
</Style> 

<Style x:Key="NavLinksBorderStyle" TargetType="Border" BasedOn="{StaticResource LinksBorderStyle}"> 
    <Setter Property="Margin" Value="33,8,33,0"/> 
</Style> 
+0

你可以包含NavContentFrameStyle风格的代码(从App.xaml或合并ResourceDictionary)? – foson 2011-03-07 20:08:59

+0

NavContentFrameStyle风格位于Assets/Styles.xaml – foson 2011-03-07 20:21:30

回答

1

这取决于你的UriMapper的样子。

因为我不喜欢斜杠后在地址栏中#,我的是:

<Style x:Key="NavContentFrameStyle" TargetType="navigation:Frame" BasedOn="{StaticResource ContentFrameStyle}"> 
    <Setter Property="UriMapper"> 
     <Setter.Value> 
      <uriMapper:UriMapper> 
       <uriMapper:UriMapping MappedUri="/Views/Home.xaml" Uri="" /> 
       <uriMapper:UriMapping MappedUri="/Views/{pageName}.xaml" Uri="{}{pageName}" /> 
      </uriMapper:UriMapper> 
     </Setter.Value> 
    </Setter> 
</Style> 

要导航到在UriMapper所列的URI之一。通过上面我的代码,你只需要使用

this.NavigationService.Navigate(new Uri("About", UriKind.Relative)); 

出的现成的样式在VS模板Uri="/{pageName}"。如果你还没有改变它,你会导航到new Uri("/About", UriKind.Relative)

+0

谢谢,我会试试这个并报告回来! – Sean 2011-03-07 20:55:37

+0

谢谢!!!!!! – Sean 2011-03-08 15:20:41

0

尝试指定UriKind.Relative。我发现在使用导航时我必须这样做。

this.NavigationService.Navigate(new Uri("/Views/About.xaml", UriKind.Relative)); 
+0

仍然没有运气。显然这个页面找不到。我正在使用默认模板,所以我肯定URI字符串指向正确的xaml。 – Sean 2011-03-07 20:23:04

+0

看到你的UriMappings后,我相信玻色子已经发布了你的解决方案的其余部分:) – grimus 2011-03-07 20:44:22