0

我在框架上加载的 xaml 页面中有以下代码。当我加载此页面时应用程序崩溃并且调试告诉我在线上有错误<Grid.Resources>。请帮忙指出我哪里出错了

<Page x:Class="Milestones.Decade2"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:s="http://schemas.microsoft.com/surface/2008"
      xmlns:debug="clr-namespace:System.Diagnostics;assembly=WindowsBase"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
    Title="Decade2">
    <Grid DockPanel.Dock="Left" Width="Auto" Height="Auto" >

        <Grid.Resources>
            <!-- The DataTemplate for a <Milestone> element. -->
            <XmlDataProvider Source="Data/Decade2.xml" XPath="Milestones" x:Key="AllMilestones" IsInitialLoadEnabled="True"/>
            <DataTemplate DataType="Milestone" x:Key="Milestones">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding XPath=@Name}" />
                </StackPanel>
            </DataTemplate>
            <DataTemplate DataType="Milestone" x:Key="View">
                <StackPanel FlowDirection="LeftToRight" Orientation="Horizontal">
                    <Image Source="{Binding XPath=./Media}" Width="250" Height="250" Grid.Column="0" />
                    <Canvas Grid.Column="1" Background="#0071bc" Height="Auto" >
                        <TextBlock TextWrapping="Wrap" Width="600">
                    <TextBlock TextWrapping="Wrap" FontSize="56" FontFamily="TitilliumText22L XBold" Text="{Binding XPath=@Name}" Padding="5" Foreground="White"  />
                    <TextBlock TextWrapping="Wrap" FontSize="42" FontFamily="TitilliumText22L" FontWeight="Bold" Text="{Binding XPath=./Introduction}"  Padding="5" Foreground="White"/>
                    <TextBlock TextWrapping="Wrap" FontSize="36" FontFamily="TitilliumText22L" Text="{Binding XPath=./Description}" Padding="5" Foreground="White" />
                   </TextBlock>
                    </Canvas>
                </StackPanel>
            </DataTemplate>

        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <!-- The top row is for the ListBox. -->
            <RowDefinition Height="Auto" />
            <!-- The bottom row is for the Image. -->
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <s:SurfaceListBox Grid.Row="3" IsSynchronizedWithCurrentItem="True"  ItemsSource="Binding Source={StaticResource AllMilestones}, XPath=Milestones" ItemTemplate="{StaticResource Milestones}" Name="lstDecades2">
            <s:SurfaceListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </s:SurfaceListBox.ItemsPanel>
        </s:SurfaceListBox>

        <!-- Displays the selected person's photo and name. -->
        <DockPanel Grid.Row="1">
            <ScrollViewer>

                <ListBox Name="lbFiles" 
                 ItemsSource="{Binding ElementName=lstDecades2, Path=SelectedItem, UpdateSourceTrigger=PropertyChanged}"                      
                  />

            </ScrollViewer>
        </DockPanel>

        <Image Source="Resources/images/logo.jpg" Height="186" Grid.Column="0" Grid.Row="0"></Image>
    </Grid>
</Page>
4

2 回答 2

0

切换 Key 和 DataType 字段,错误是说您为相同的 DataType 添加了 2 个资源。

<DataTemplate  x:Key="Milestones" DataType="Milestone">
</DataTemplate>

<DataTemplate x:Key="View" DataType="Milestone">
</DataTemplate>
于 2012-06-12T07:48:42.593 回答
0

我按照安迪的指示进行了更改,页面加载但列表框为空白。下面是代码和XML

    <Grid.Resources>
        <!-- The DataTemplate for a <Milestone> element. -->
        <XmlDataProvider Source="Data/Decade2.xml" XPath="Milestones" x:Key="AllMilestones" IsInitialLoadEnabled="True"/>
        <DataTemplate x:Key="Milestones" DataType="Milestone" >

                <TextBlock Text="{Binding XPath=@Name}" />

        </DataTemplate>
        <DataTemplate x:Key="View" DataType="View" >
            <StackPanel FlowDirection="LeftToRight" Orientation="Horizontal">
                <Image Source="{Binding XPath=./Media}" Width="250" Height="250" Grid.Column="0" />
                <Canvas Grid.Column="1" Background="#0071bc" Height="Auto" >
                    <TextBlock TextWrapping="Wrap" Width="600">
                <TextBlock TextWrapping="Wrap" FontSize="56" FontFamily="TitilliumText22L XBold" Text="{Binding XPath=@Name}" Padding="5" Foreground="White"  />
                <TextBlock TextWrapping="Wrap" FontSize="42" FontFamily="TitilliumText22L" FontWeight="Bold" Text="{Binding XPath=./Introduction}"  Padding="5" Foreground="White"/>
                <TextBlock TextWrapping="Wrap" FontSize="36" FontFamily="TitilliumText22L" Text="{Binding XPath=./Description}" Padding="5" Foreground="White" />
               </TextBlock>
                </Canvas>
            </StackPanel>
        </DataTemplate>

    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <!-- The top row is for the ListBox. -->
        <RowDefinition Height="Auto" />
        <!-- The bottom row is for the Image. -->
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <s:SurfaceListBox Grid.Row="3" IsSynchronizedWithCurrentItem="True"  ItemsSource="Binding Source={StaticResource AllMilestones}, XPath=Milestones" ItemTemplate="{StaticResource Milestones}" Name="lstDecades2">
        <s:SurfaceListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </s:SurfaceListBox.ItemsPanel>
    </s:SurfaceListBox>

    <!-- Displays the selected person's photo and name. -->
    <DockPanel Grid.Row="1">
        <ScrollViewer>

            <ListBox Name="lbFiles" 
             ItemsSource="{Binding ElementName=lstDecades2, Path=SelectedItem, UpdateSourceTrigger=PropertyChanged}"                      
              />

        </ScrollViewer>
    </DockPanel>

    <Image Source="Resources/images/logo.jpg" Height="186" Grid.Column="0" Grid.Row="0"></Image>
</Grid>

XML

<Milestones>
  <Milestone Name="1987">
    <View>
      <Media></Media>
      <MediaDescription></MediaDescription>
      <Introduction></Introduction>
      <Description></Description>
    </View>
    <View>
      <Media></Media>
      <MediaDescription></MediaDescription>
      <Introduction></Introduction>
      <Description></Description>
    </View>
  </Milestone>
<Milestone Name="1988">
    <View>
      <Media></Media>
      <MediaDescription></MediaDescription>
      <Introduction></Introduction>
      <Description></Description>
    </View>
    <View>
      <Media></Media>
      <MediaDescription></MediaDescription>
      <Introduction></Introduction>
      <Description></Description>
    </View>
  </Milestone>
</Milestones>
于 2012-06-13T12:51:29.333 回答