WPF应用程序崩溃

时间:2012-06-12 06:05:46

标签: wpf xaml

我在框架中加载的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>

2 个答案:

答案 0 :(得分:0)

切换Key和DataType字段,错误表示您为同一DataType添加了2个资源。

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

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

答案 1 :(得分:0)

我按照Andy的指示进行了更改并且页面加载但是列表框是空白的。下面是代码和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>