2012-06-12 28 views
0

我在我的xaml頁面中加載了一個框架中的以下代碼。當我加載這個頁面時,應用程序崩潰並且調試告訴我在線<Grid.Resources>上有錯誤。請指出幫我要去哪裏錯了WPF App Crashing

<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 [email protected]}" /> 
       </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 [email protected]}" 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

開關的鑰匙和周圍的數據類型的字段,錯誤是說你已經添加2個資源,爲相同的數據類型。

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

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

感謝它的工作,但是,我很困惑與所有的鍵和DataTpe。有人可以指導我如何加載正確的和在哪裏。這裏是我的xml <里程碑名稱= 「1987」> <簡介> <前言>

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 [email protected]}" /> 

     </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 [email protected]}" 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>