Dot-Net

錯誤 - 在 WPF 應用程序中找不到靜態資源

  • October 8, 2018

我正在學習 WPF 並從這個MSDN 教程開始。

我只是按照教程。當我按照教程完成程式碼並嘗試執行時,我在 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 XPath=@Name}"/>
           </DataTemplate>
       </Grid.Resources>
   </Grid>
</Page>

我在網格資源中有所需的模板,因此將其添加為靜態資源。儘管如此,它還是會拋出數據模板不可用的異常。

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

這個錯誤有幾個原因。我的問題的解決方案是我未能添加“InitializeComponent();” 因此,在 Application 的建構子中,包含 ResourceDictionary 的 Xaml 從未被初始化。因此錯誤“找不到…”我沒有提到我是手動編碼的。如果您通過 Visual Studio 生成程式碼,則不需要這樣做。

引用自:https://stackoverflow.com/questions/2914211