2010-05-26 90 views
11

我在學習WPF,並以this MSDN教程開始。錯誤 - 在WPF應用程序中找不到靜態資源

我只是按照教程。當我的代碼完成按教程並嘗試運行我得到一個XAML頁面它說

System.Windows.StaticResourceExtension「上提供價值‘異常’拋出一個異常」。行號'27'和行位置'55'「,內部異常顯示錯誤是」無法找到名爲'personItemTemplate'的資源。資源名稱是區分大小寫的。「

罪魁禍首XAML如下。

<Page x:Class="ExpenseIt.ExpenseItHome" 
     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" 
     mc:Ignorable="d" 
     d:DesignHeight="321" d:DesignWidth="532" 
    Title="ExpenseIt - Home"> 

    <Grid Margin="10,0,10,10"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="230" /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <Label Grid.Column="1" Style="{StaticResource headerTextStyle}">View Expense Report</Label> 
     <!-- Resource List Label--> 
     <Border Grid.Column="1" Grid.Row="1" Style="{StaticResource listHeaderStyle}"> 
      <Label VerticalAlignment="Center" Foreground="White" FontWeight="Bold">Names</Label> 
     </Border> 
     <!-- Resource List--> 
     <ListBox Name="peopleListBox" Grid.Column="1" Grid.Row="2" 
     ItemsSource="{Binding Source={StaticResource ExpenseDataSource}, XPath=Person}" 
     ItemTemplate="{StaticResource personItemTemplate}"> 
     </ListBox> 

     <!-- View button --> 
     <Button Grid.Column="1" Grid.Row="3" Click="Button_Click" Style="{StaticResource buttonStyle}">View</Button> 

     <!-- Set Background Image--> 
     <Grid.Background> 
      <ImageBrush ImageSource="watermark.png" /> 
     </Grid.Background> 
     <Grid.Resources> 

      <!-- Expense Report Data --> 
      <XmlDataProvider x:Key="ExpenseDataSource" XPath="Expenses"> 
       <x:XData> 
        <Expenses xmlns=""> 
         <Person Name="TommyVance" Department="Legal"> 
          <Expense ExpenseType="Lunch" ExpenseAmount="50" /> 
          <Expense ExpenseType="Transportation" ExpenseAmount="50" /> 
         </Person> 
         <Person Name="PhilJackson" Department="Marketing"> 
          <Expense ExpenseType="Document printing" 
     ExpenseAmount="50"/> 
          <Expense ExpenseType="Gift" ExpenseAmount="125" /> 
         </Person> 
         <Person Name="PaulBriggs" Department="Engineering"> 
          <Expense ExpenseType="Magazine subscription" 
    ExpenseAmount="50"/> 
          <Expense ExpenseType="New machine" ExpenseAmount="600" /> 
          <Expense ExpenseType="Software" ExpenseAmount="500" /> 
         </Person> 
         <Person Name="AlfredNobel" Department="Finance"> 
          <Expense ExpenseType="Dinner" ExpenseAmount="100" /> 
         </Person> 
        </Expenses> 
       </x:XData> 
      </XmlDataProvider> 
      <!-- Data Template to mention that Name should be fetched from the XMLDataProvider --> 
      <!-- Name item template --> 
      <DataTemplate x:Key="personItemTemplate"> 
       <Label Content="{Binding [email protected]}"/> 
      </DataTemplate> 
     </Grid.Resources> 
    </Grid> 
</Page> 

我有網資源內所需的模板,因此將其添加爲一個靜態的資源。儘管如此,它拋出但數據模板不可用的例外

回答

28

<Grid.Resources> ... </Grid.Resources>移動到您的網格定義的頂部,它將工作DataTemplate似乎需要在它被引用之前被定義我將您的示例複製到一個應用程序並確認向上移動資源部分解決了這個問題。

+0

是的,我試着隨機剛纔來到這裏來更新答案。但你是第一個。 :-)我接受你的答案。爲什麼資源引用的行爲如此? – blntechie 2010-05-26 15:33:39

+0

嗯,我不確定。它看起來一眼就可以按照順序解析XAML,所以它不知道'personItemTemplate'在第一次被引用時是什麼。然而,它確實知道你的'ExpenseDataSource'是什麼,儘管它放置在哪裏。所以......我將不得不推遲到有更多專業知識的人才能得到更好的解釋。 :) – 2010-05-26 20:24:46

+0

Thanks.I只是討厭這個WPF :-D – 2012-07-09 14:43:22

3

這個錯誤有幾個原因。我的proplem解決方案是我沒有添加一個「InitializeComponent();」在Application的構造函數中,因此包含ResourceDictionary的Xaml從未初始化。因此錯誤「找不到......」我沒有提到我是手工編碼。如果通過Visual Studio生成代碼,這不是必需的。