.net 错误 - 在 WPF 应用程序中找不到静态资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2914211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error - Cannot find static resource in a WPF application
提问by blntechie
I'm learning WPF and started with thisMSDN tutorial.
我正在学习 WPF 并从这个MSDN 教程开始。
I was just following the tutorial. When I finished the code as per the tutorial and try to run I get an exception in a XAML page which says
我只是按照教程。当我按照教程完成代码并尝试运行时,我在 XAML 页面中收到一个异常,上面写着
'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '27' and line position '55'." . And inner exception reveals that error is "Cannot find resource named 'personItemTemplate'. Resource names are case sensitive.".
'在'System.Windows.StaticResourceExtension' 上提供值引发异常。行号'27'和行位置'55'。”。内部异常显示错误是“找不到名为'personItemTemplate'的资源。资源名称区分大小写。”。
The culprit XAML is below.
罪魁祸首 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="PhilHymanson" 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>
I have the required template inside the Grid resources and so adding it as a static resource. Still, it throws the exception that the datatemplate is not available.
我在 Grid 资源中有所需的模板,因此将其添加为静态资源。尽管如此,它仍会引发数据模板不可用的异常。
回答by Ben Collier
Move the <Grid.Resources> ... </Grid.Resources>to the top of your grid definition and it will work. The DataTemplate appears to need to be defined before it is referenced. I copied your sample into an app and confirmed that moving the Resources section up solved the problem.
将 移动<Grid.Resources> ... </Grid.Resources>到网格定义的顶部,它将起作用。DataTemplate 似乎需要在引用之前定义。我将您的示例复制到一个应用程序中,并确认将资源部分向上移动解决了问题。
回答by Ananize Scott
There are several reasons for this error. The solution to my proplem was that I failed to add a "InitializeComponent();" in the constructor of Application therefore the Xaml containing the ResourceDictionary was never initialized. Hence the error "Can not find..." I failed to mention that I am hand-coding. If you generating code via Visual Studio, this is not required.
此错误有多种原因。我的问题的解决方案是我未能添加“InitializeComponent();” 在 Application 的构造函数中,因此从未初始化包含 ResourceDictionary 的 Xaml。因此错误“找不到...”我没有提到我是手工编码的。如果您通过 Visual Studio 生成代码,这不是必需的。
回答by Eirik Lohne Bakken
I got the same error, but none of the answers above worked.
我遇到了同样的错误,但上面的答案都没有奏效。
My error got solved by changing the XAML on the datatemplate from :
通过从以下位置更改数据模板上的 XAML,我的错误得到了解决:
<DataTemplate DataType="local:DtoDmParent" x:Key="dataTemplateDtoDmParent" >
<TextBlock Text="test"/>
</DataTemplate>
to
到
<DataTemplate x:Key="dataTemplateDtoDmParent" DataType="local:DtoDmParent" >
<TextBlock Text="test"/>
</DataTemplate>
Changing so the Datatype parameter was after the x:key parameter
更改数据类型参数在 x:key 参数之后

