c# – 资源无法解析
发布时间:2020-12-16 02:01:35 所属栏目:百科 来源:网络整理
导读:我收到错误“资源”ComponentTypeToStringConverter无法解决“有人能告诉我我做错了什么? 我有这个组合框: ComboBox SelectedItem="{Binding PcComponent.ComponentTypeName}" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" ComboBox.ItemTemplate Da
我收到错误“资源”ComponentTypeToStringConverter无法解决“有人能告诉我我做错了什么?
我有这个组合框: <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> 资源: <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(名称空间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,System.Globalization.CultureInfo culture) { throw new NotSupportedException(); } } 解决方法
在设计时.您的资源尚未编译,并且您拥有ComboBox的当前XAML文件无法看到ResourceDictionary.
假设您的资源是在App.xaml中定义的,那么当您在运行时运行它时,它应该没有任何问题,因为它将能够找到密钥. 如果你想摆脱设计时的错误,你可以做的就是在ComboBox所在的XAML文件中,你可以添加ResourceDictioanry以便它能够找到它. 假设这是一个Window,并且您的ResourceDictionary未在App.xaml中定义,而是作为单独的文件 <Window.Resources> <ResourceDictionary Source=""/> </Window.Resources> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |