Запутался в навигации Silverlight - PullRequest
0 голосов
/ 07 марта 2011

Многие вещи в 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 ]

1 голос
/ 07 марта 2011

Это зависит от того, как выглядит ваш 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>

Вы хотите перейти к одному из Uri, перечисленных в UriMapper. С моим кодом выше, вы просто используете

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

Стиль «из коробки» в шаблоне VS - Uri="/{pageName}". Если вы не изменили его, перейдите на new Uri("/About", UriKind.Relative)

0 голосов
/ 07 марта 2011

Попробуйте указать UriKind.Relative. Я обнаружил, что должен делать это при работе с навигацией.

this.NavigationService.Navigate(new Uri("/Views/About.xaml", UriKind.Relative));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...