wpf 资源无法解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23550838/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:41:24  来源:igfitidea点击:

Resource could not be resolved

c#wpfxaml

提问by Notur

I got a error "The resource "ComponentTypeToStringConverter could not be resolved" Can someone tell me what am I doing wrong?

我收到错误消息“资源“无法解析 ComponentTypeToStringConverter” 谁能告诉我我做错了什么?

I've got this combo box:

我有这个组合框:

<ComboBox SelectedItem="{Binding PcComponent.ComponentTypeName}" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2">
      <ComboBox.ItemTemplate>
           <DataTemplate>
                 <TextBlock Text="{Binding Converter={StaticResource ComponentTypeToStringConverter}}"/> <!-- HERE I got a error The resource "ComponentTypeToStringConverter could not be resolved -->
           </DataTemplate>
      </ComboBox.ItemTemplate>
</ComboBox>

Resource:

资源:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:c="clr-namespace:PcConfigurator.Converters">
    <c:ComponentTypeToStringConverter x:Key="ComponentTypeToStringConverter"/>
</ResourceDictionary>

Converter (namespace PcConfigurator.Converters):

转换器(命名空间 PcConfigurator.Converters):

public class ComponentTypeToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is ComponentType)) { return null; }

            ComponentType type = (ComponentType)value;

            switch (type)
            {
               //Do something
            }

            throw new InvalidOperationException("Enum value is unknown");
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
}

回答by 123 456 789 0

In design time. Your resources are not yet compiled and the current XAMLfile that you have where it has the ComboBoxcan't see the ResourceDictionary.

设计时。您的资源尚未编译,并且XAML您拥有的当前文件ComboBox无法看到ResourceDictionary.

Assuming your resources is defined in App.xamlthen you should have no problems with it when you run it in runtime as it'll be able to find the key.

假设你的资源是在 中定义的,App.xaml那么当你在运行时运行它时你应该没有问题,因为它能够找到key.

If you want to get rid of the error in design timethen what you can do is on your XAMLfile where the ComboBoxlives, you can add the ResourceDictioanryso that it'll be able to find it.

如果您想摆脱设计时的错误,那么您可以做的是在您的XAML文件中存在的ComboBox地方,您可以添加ResourceDictioanry以便它能够找到它。

Assuming this is a Windowand you have ResourceDictionarythat is not defined in the App.xamlbut as a separate file

假设这是一个Window并且您ResourceDictionary没有在该App.xaml文件中定义它,而是将其定义为一个单独的文件

<Window.Resources>
 <ResourceDictionary Source=""/>
</Window.Resources>