[英]Newbie error: XamlParseException occured in WPF application
我嘗試使用以下示例構建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>
我對我做錯了什么以及如何糾正它有任何幫助,將不勝感激! 謝謝!
此錯誤表明未找到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}">
我遇到了具有相同示例代碼的相同問題-上面“在頂層網格下方”答案中的術語使我感到困惑,因為示例中的數據提供程序位於頂層網格內,如下所示:
<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>
請享用!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.