错误 - 在WPF应用程序中找不到静态资源
问题描述:
我在学习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>
我有网资源内所需的模板,因此将其添加为一个静态的资源。尽管如此,它抛出但数据模板不可用的例外
答
将<Grid.Resources> ... </Grid.Resources>
移动到您的网格定义的顶部,它将工作DataTemplate似乎需要在它被引用之前被定义我将您的示例复制到一个应用程序并确认向上移动资源部分解决了这个问题。
答
这个错误有几个原因。我的proplem解决方案是我没有添加一个“InitializeComponent();”在Application的构造函数中,因此包含ResourceDictionary的Xaml从未初始化。因此错误“找不到......”我没有提到我是手工编码。如果通过Visual Studio生成代码,这不是必需的。
是的,我试着随机刚才来到这里来更新答案。但你是第一个。 :-)我接受你的答案。为什么资源引用的行为如此? – blntechie 2010-05-26 15:33:39
嗯,我不确定。它看起来一眼就可以按照顺序解析XAML,所以它不知道'personItemTemplate'在第一次被引用时是什么。然而,它确实知道你的'ExpenseDataSource'是什么,尽管它放置在哪里。所以......我将不得不推迟到有更多专业知识的人才能得到更好的解释。 :) – 2010-05-26 20:24:46
Thanks.I只是讨厌这个WPF :-D – 2012-07-09 14:43:22