对Silverlight Navigation感到困惑

时间:2011-03-07 19:54:19

标签: silverlight

Silverlight中的很多东西看起来都很简单......这就是其中之一。我有下面的MainPage xaml的代码示例,以及之后的Home.xaml。

<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>

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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

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