2

我正在尝试使用此处的示例构建 WPF 应用程序:Windows 示例 WPF 应用程序 。但是,当我完成它时,我遇到了以下错误

'在 'System.Windows.StaticResourceExtension' 上提供值引发了异常。' 行号“36”和行位置“55”。

并将其扔在以下行

 <ListBox Name="peopleListBox" Grid.Column="1" Grid.Row="2" 
         ItemsSource="{Binding Source={StaticResource ExpenseDataSource}, XPath=Person}"
         ItemTemplate="{StaticResource nameItemTemplate}">
        </ListBox>

内部异常提供以下详细信息

{“找不到名为 'nameItemTemplate' 的资源。资源名称区分大小写。”}

这是有问题的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="300" d:DesignWidth="300"
    Title="ExpenseIt-Home">

    <Grid Margin="10,0,10,10">
        <Grid.Background>
            <ImageBrush ImageSource="2012-12-20 13.27.57.jpg"  />
        </Grid.Background>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="230" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- People list -->

        <Label Grid.Column="1" Style="{StaticResource headerTextStyle}" >
            View Expense Report
        </Label>

        <Border Grid.Column="1" Grid.Row="1" Style="{StaticResource listHeaderStyle}">
            <Label Style="{StaticResource listHeaderTextStyle}">Names</Label>
        </Border>
        <ListBox Name="peopleListBox" Grid.Column="1" Grid.Row="2" 
         ItemsSource="{Binding Source={StaticResource ExpenseDataSource}, XPath=Person}"
         ItemTemplate="{StaticResource nameItemTemplate}">
        </ListBox>



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

            <!-- Expense Report Data -->
            <XmlDataProvider x:Key="ExpenseDataSource" XPath="Expenses">
                <x:XData>
                    <Expenses xmlns="">
                        <Person Name="Mike" Department="Legal">
                            <Expense ExpenseType="Lunch" ExpenseAmount="50" />
                            <Expense ExpenseType="Transportation" ExpenseAmount="50" />
                        </Person>
                        <Person Name="Lisa" Department="Marketing">
                            <Expense ExpenseType="Document printing"
      ExpenseAmount="50"/>
                            <Expense ExpenseType="Gift" ExpenseAmount="125" />
                        </Person>
                        <Person Name="John" Department="Engineering">
                            <Expense ExpenseType="Magazine subscription" 
     ExpenseAmount="50"/>
                            <Expense ExpenseType="New machine" ExpenseAmount="600" />
                            <Expense ExpenseType="Software" ExpenseAmount="500" />
                        </Person>
                        <Person Name="Mary" Department="Finance">
                            <Expense ExpenseType="Dinner" ExpenseAmount="100" />
                        </Person>
                    </Expenses>
                </x:XData>
            </XmlDataProvider>


            <!-- Name item template -->
            <DataTemplate x:Key="nameItemTemplate">
                <Label Content="{Binding XPath=@Name}"/>// isnt this enough?
            </DataTemplate> 
        </Grid.Resources>
    </Grid>
</Page>

任何关于我做错了什么以及如何纠正它的帮助,将不胜感激!谢谢!

4

2 回答 2

4

此错误表示未找到 StaticResource 键。使用静态资源时,必须在引用之前定义它们,无论是在 App.Xaml 中还是在当前文件中。

因此,请确保示例中的“ExpenseDataSource”和“nameItemTemplate”包含在顶级 Grid.Resources 下:

<Page ...>
    <Grid ...>
        <Grid.Resources>
            <!-- Name item template -->
            <DataTemplate x:Key="nameItemTemplate">
                <Label Content="{Binding XPath=@Name}"/>
            </DataTemplate>

            <!-- Expense Report Data -->
            <XmlDataProvider x:Key="ExpenseDataSource" XPath="Expenses"> 
                ...

        </Grid.Resources>

        <ListBox Name="peopleListBox" 
            ItemsSource="{Binding Source={StaticResource ExpenseDataSource}, XPath=Person}"
            ItemTemplate="{StaticResource nameItemTemplate}">
于 2013-09-18T20:00:41.200 回答
0

我使用相同的示例代码遇到了同样的问题 - 上面答案中的术语“在顶级网格下”让我感到困惑,因为数据提供者在示例中的顶级网格内,如下所示:

<Page>
  <Grid>
    <!-- other markup that uses the Resources -->

    <Grid.Resources>
      <!-- resources stuff as above -->
    </Grid.Resources>
  </Grid>
</Page>

真正的问题是由 XAML 代码的自上而下解析引起的……显然是单遍解析器,而不是两遍解析器(用于其他标记)。知道这一点的解决方案是移动网格静态资源(数据提供者),使其在物理上位于标记中引用的位置之上。重新排序标记,使其更像以下内容,并且可以正常工作……这个令人困惑:

<Page>
  <Grid>
    <Grid.Resources>
      <!-- resources stuff as above -->
      <!-- Grid.Resources MUST occur ABOVE the other markup that uses resources -->
    </Grid.Resources>

    <!-- other markup that uses the Resources -->
  </Grid>
</Page>

享受!

于 2016-04-13T16:45:48.373 回答