我的自定义值转换器导致XAML验证工具失败

时间:2015-04-24 19:14:50

标签: c# wpf xaml ivalueconverter

我创建了一个自定义转换器,可根据配置的映射执行值转换。它看起来像下面

public class UniversalConverter : List<ConverterItem>, IValueConverter
{
    private bool useDefaultValue;

    private object defaultValue;

    public object DefaultValue
    {
        get { return defaultValue; }
        set
        {
            defaultValue = value;
            useDefaultValue = true;
        }
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        foreach (var item in this)
            if (Equals(item.From, value))
                return item.To;
        if (useDefaultValue)
            return DefaultValue;
        throw new ConversionException(string.Format("Value {0} can't be converted and default value is not allowed", value));
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        foreach (var item in this)
            if (Equals(item.To, value))
                return item.From;
        throw new ConversionException(string.Format("Value {0} can't be converted back", value));
    }
}

public class ConverterItem
{
    public object From { get; set; }

    public object To { get; set; }
}

public class ConversionException : Exception
{
    public ConversionException() { }

    public ConversionException(string message) : base(message) { }
}

示例XAML在

之下
<core:UniversalConverter x:Key="ItemCountToVisiblityConverter" DefaultValue="{x:Static Visibility.Collapsed}">
    <core:ConverterItem To="{x:Static Visibility.Visible}">
        <core:ConverterItem.From>
            <system:Int32>0</system:Int32>
        </core:ConverterItem.From>
    </core:ConverterItem>
</core:UniversalConverter>

现在一切都构建并且工作正常,但如果我使用它,XAML Visual Studio会用弯曲的蓝线强调整个文件并显示两种错误:

1)如果将转换器放入ResourceDictionary并分配了x:Key属性,则会显示Missing key value on 'UniversalConverter' object

2)如果我为DefaultValue属性分配任何值(例如{x:Null}),则邮件为XAML Node Stream: Missing EndMember for 'StuffLib.UniversalConverter.{http://schemas.microsoft.com/winfx/2006/xaml}_Items' before StartMember 'StuffLib.UniversalConverter.DefaultValue'

这些消息的原因是什么?我可以和他们住在一起,但他们隐藏了所有其他编译器和ReSharper标记

3 个答案:

答案 0 :(得分:3)

不要从列表继承,只需在转换器中创建Items属性:

[ContentProperty("Items")]
public class UniversalConverter : IValueConverter
{
    public ConverterItem[] Items { get; set; }

    public object DefaultValue { get; set; }
    //all other stuff goes here
}

和xaml:

<l:UniversalConverter x:Key="MyConverter">
  <x:Array Type="l:ConverterItem">
    <l:ConverterItem From="..." To="..." />

答案 1 :(得分:2)

根据@Leiro给出的答案

[ContentProperty("Items")]
public class UniversalConverter : IValueConverter
{
    public UniversalConverter()
    {
        Items = new List<ConverterItem>();
    }

    public List<ConverterItem> Items { get; private set; } 
    //All other logic is the same
}

请注意,这样您就不需要在XAML中将项目包装在集合中

产生的XAML

<core:UniversalConverter x:Key="ItemCountToVisiblityConverter" DefaultValue="{x:Static Visibility.Collapsed}">
    <core:ConverterItem To="{x:Static Visibility.Visible}">
        <core:ConverterItem.From>
            <system:Int32>0</system:Int32>
        </core:ConverterItem.From>
    </core:ConverterItem>
</core:UniversalConverter>

答案 2 :(得分:0)

因为它在设计时使用但没有数据所以我怀疑是抛出了NullReferenceException。尝试在IValueConverter.Convert()方法体的顶部检查设计时模式,如下所示:

// Check for design mode. 
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)) 
{
    return false;
}