【问题标题】:Error - Cannot find static resource in a WPF application错误 - 在 WPF 应用程序中找不到静态资源
【发布时间】:2011-02-24 05:54:44
【问题描述】:

我正在学习 WPF,并从 this 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>

我在网格资源中有所需的模板,因此将其添加为静态资源。尽管如此,它还是会抛出数据模板不可用的异常。

【问题讨论】:

    标签: .net wpf xaml .net-4.0


    【解决方案1】:

    &lt;Grid.Resources&gt; ... &lt;/Grid.Resources&gt; 移动到网格定义的顶部,它将起作用。 DataTemplate 似乎需要在被引用之前定义。我将您的示例复制到一个应用程序中,并确认将“资源”部分向上移动可以解决问题。

    【讨论】:

    • 是的,我刚才随机尝试了一下,来到这里更新答案。但你是第一个。 :-) 我接受你的回答。资源引用为什么会这样?
    • 嗯,我不确定。乍一看,它似乎按顺序解析 XAML,因此它不知道第一次引用时的“personItemTemplate”是什么。但是,它确实知道您的“ExpenseDataSource”是什么,尽管它位于何处。所以......我将不得不向更专业的人寻求更好的解释。 :)
    • 谢谢。我只是讨厌 WPF :-D
    • 如果您想要更动态的体验,请使用 DynamicResource。 StaticResource 资源更快并防止循环引用。实际上并不是这么简单,而是将 StaticResource 视为编译时查找,将 DynamicResource 视为运行时反射查找。 DynamicResource “更好”,但您需要为运行时的灵活性付费。当它可以工作时,首选 StaticResource。
    【解决方案2】:

    这个错误有几个原因。我的问题的解决方案是我未能添加“InitializeComponent();”因此,在 Application 的构造函数中,包含 ResourceDictionary 的 Xaml 从未被初始化。因此错误“找不到...”我没有提到我是手动编码的。如果您通过 Visual Studio 生成代码,则不需要。

    【讨论】:

      【解决方案3】:

      我遇到了同样的错误,但以上答案均无效。

      我的错误通过将数据模板上的 XAML 更改为:

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

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

      将 Datatype 参数更改为在 x:key 参数之后

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-05
        • 1970-01-01
        • 2015-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-27
        相关资源
        最近更新 更多