错误 - 在WPF应用程序中找不到静态资源

13

我正在学习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资源中拥有所需的模板,并将其添加为静态资源。但仍然会抛出数据模板不可用的异常。

3个回答

29

<Grid.Resources> ... </Grid.Resources>移到你的网格定义顶部,它就会起作用。看起来需要在引用之前定义DataTemplate。我将你的样本复制到应用程序中,并确认将资源部分向上移动解决了问题。


是的,我刚刚随机尝试了一下,然后来这里更新答案。但你先回答了。 :-) 我接受你的答案。为什么资源引用会出现这种情况呢? - lakshminb7
嗯,我不确定。乍一看,它似乎按顺序解析XAML,因此当它首次引用'personItemTemplate'时,它不知道它是什么。但是,不管它放在哪里,它确实知道你的'ExpenseDataSource'是什么。所以...我将不得不把更多专业知识的问题推迟给别人来进行更好的解释。 :) - Ben Collier
谢谢。我只是因为这个讨厌WPF :-D - Mehdi LAMRANI
1
如果您想要更加动态的体验,请使用DynamicResource。StaticResource资源更快,可以防止循环引用。虽然情况并不是这么简单,但可以将StaticResource视为编译时查找,将DynamicResource视为运行时反射查找。DynamicResource“更好”,但您需要在运行时付出灵活性的代价。如果StaticResource能够胜任,请优先选择它。 - PatrickV

5

我遇到了相同的错误,但是上面的答案都没有解决问题。

我通过更改datatemplate中XAML的方式解决了我的错误:

<DataTemplate DataType="local:DtoDmParent" x:Key="dataTemplateDtoDmParent"  >
    <TextBlock Text="test"/>
</DataTemplate>

to

<DataTemplate x:Key="dataTemplateDtoDmParent" DataType="local:DtoDmParent" >
    <TextBlock Text="test"/>
</DataTemplate>

更改Datatype参数,使其在x:key参数后面


5

这个错误有几个可能的原因。我遇到的问题是,在Application的构造函数中没有添加"InitializeComponent();",导致包含ResourceDictionary的Xaml文件没有被初始化。因此出现了“找不到...”的错误提示。需要注意的是,如果你是通过Visual Studio生成代码,则不需要手动添加这一行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接