找不到名为“X"的资源,为什么不呢? [英] Cannot find resource named 'X', why not?

查看:43
本文介绍了找不到名为“X"的资源,为什么不呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为库存管理创建一个向导 UI.xaml 中的相关行是:

I want to create a wizard UI for inventories management. The relevant line in the xaml is:

<ContentPresenter Content="{Binding Current}" ContentTemplateSelector="{StaticResource inventorySelector}"/>

Current"是当前活动的视图模型,AvailableInventoriesViewModel、GroupsViewModel、NewArticlesViewModel、ResultViewModel 之一.我定义的 DataTemplateSelector 如下:

"Current" is the currently active view model, one of AvailableInventoriesViewModel, GroupsViewModel, NewArticlesViewModel, ResultViewModel. The DataTemplateSelector I have defined as such:

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using Centron.WPF.WarehousingExtension.InventoryModule.ViewModels.WizardViewModels;

namespace Centron.WPF.WarehousingExtension.InventoryModule.UI.DataTemplateSelectors
{
    public class InventoryDatatemplateSelector : DataTemplateSelector
    {
        public DataTemplate AvailableDatatype { get; set; }
        public DataTemplate GroupsDatatype { get; set; }
        public DataTemplate NewDatatype { get; set; }
        public DataTemplate ResultDatatype { get; set; }

        public InventoryDatatemplateSelector()
        {
            Debug.WriteLine("");
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item is AvailableInventoriesViewModel)
                return AvailableDatatype;
            else if (item is GroupsViewModel)
                return GroupsDatatype;
            else if (item is NewArticlesViewModel)
                return NewDatatype;
            else return ResultDatatype;
        }
    }
}

然后我像这样创建 DataTemplates 和 Selector 的实例:

Then I create instances of the DataTemplates and a Selector like this:

<base:InventoryViewBase.Resources>
    <DataTemplate DataType="viewModels:AvailableInventoriesViewModel" x:Key="availableInventoriesDatatype">
        <controls:AvailableInventoriesView />
    </DataTemplate>
    <DataTemplate DataType="viewModels:GroupsViewModel" x:Key="groupsDatatype">
        <controls:GroupsView />
    </DataTemplate>
    <DataTemplate DataType="viewModels:NewArticlesViewModel" x:Key="newArticlesDatatype">
        <controls:NewArticlesView />
    </DataTemplate>
    <DataTemplate DataType="viewModels:ResultViewModel" x:Key="resultDatatype">
        <controls:ResultView/>
    </DataTemplate>
    <selector:InventoryDatatemplateSelector
            x:Key="inventorySelector" 
            AvailableDatatype="{StaticResource availableInventoriesDatatype}"
            GroupsDatatype="{StaticResource groupsDatatype}"
            NewDatatype="{StaticResource newArticlesDatatype}"
            ResultDatatype="{StaticResource resultDatatype}"/>
</base:InventoryViewBase.Resources>

我在 InventoryDatatemplateSelector 的构造函数中设置了一个断点,并且可以单步执行,但是在下一个调试步骤中,显然当它尝试设置该选择器实例的第一个属性时,我立即收到一个带有内部异常的异常:

I set a breakpoint in the constructor of my InventoryDatatemplateSelector, and can step through it, but in the next Debug step, apparently when it tries to set the first property of that selector instance, I immediately get an exception with inner exception:

找不到名为 \"availableInventoriesDatatype\" 的资源.资源名称区分大小写.

Cannot find resource named \"availableInventoriesDatatype\". Resource names are case sensitive.

这是怎么回事,为什么资源在明确定义的情况下找不到?

What's the deal, why is the resource not found when it's clearly defined?

推荐答案

好的,我找到了解决方案.唯一的错误是必须首先设置资源的Key"属性.所以,而不是:

Ok, I found the solution. The only error was that the "Key" property of a resource has to be set first. So instead of:

    <DataTemplate DataType="viewModels:AvailableInventoriesViewModel" x:Key="availableInventoriesDatatype" >
        <controls:AvailableInventoriesView />
    </DataTemplate>

我需要:

    <DataTemplate x:Key="availableInventoriesDatatype" DataType="viewModels:AvailableInventoriesViewModel" >
        <controls:AvailableInventoriesView />
    </DataTemplate>

这篇关于找不到名为“X"的资源,为什么不呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆