1

我在我的一个班级上设置了 ContentProperty,但是当我用 XAML 编码时,它会产生 XAMLParseException。想知道是否有人可以就我能做什么提供建议。

代码背后:

[ContentProperty(Name="Converters")]
class ChainedValueConverter: DependencyObject, IValueConverter
{
    public static readonly DependencyProperty ConvertersProperty = DependencyProperty.Register("Converters", typeof(ObservableCollection<IValueConverter>), typeof(ChainedValueConverter), null);

    public ChainedValueConverter()
    {
        SetValue(ConvertersProperty, new ObservableCollection<IValueConverter>());
    }

    public ObservableCollection<IValueConverter> Converters
    {
        get
        {
            return (ObservableCollection<IValueConverter>)GetValue(ConvertersProperty);
        }
    }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        object _cValue = value;
        foreach (IValueConverter converter in Converters)
        {
            _cValue = converter.Convert(_cValue, targetType, parameter, language);
            if (_cValue == DependencyProperty.UnsetValue)
                break;
        }
        return _cValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        object _cValue = value;
        foreach (IValueConverter converter in Converters)
        {
            _cValue = converter.ConvertBack(_cValue, targetType, parameter, language);
            if (_cValue == DependencyProperty.UnsetValue)
                break;
        }
        return _cValue;
    }
}
/// <summary>
/// Converts boolean value to visibility.
/// </summary>
class BooleanToVisibilityConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return (Boolean)value ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return (Visibility)value == Visibility.Visible ? true : false;
    }
}

XAML:

<common:ChainedValueConverter x:Key="A">
            <common:BooleanToVisibilityConverter />
    </common:ChainedValueConverter>

上面的代码在运行时会产生带有 WinRT 信息的 XamlParseException:E_UNKNOWN_ERROR

4

1 回答 1

0

我真的很喜欢您对链式转换器的想法,并试图找到解决方案。看来您只需要在构造函数中初始化您的集合(创建一个新实例)。查看我的实现:

[ContentProperty(Name = "Converters")]
public class ChainedConverter : DependencyObject, IValueConverter
{
    public ChainedConverter()
    {
        Converters = new ObservableCollection<IValueConverter>();
    }

    public static readonly DependencyProperty ConvertersProperty =
        DependencyProperty.Register("Converters", typeof (ObservableCollection<IValueConverter>), 
        typeof (ChainedConverter), new PropertyMetadata(default(ObservableCollection<IValueConverter>)));

    public ObservableCollection<IValueConverter> Converters
    {
        get { return (ObservableCollection<IValueConverter>) GetValue(ConvertersProperty); }
        set { SetValue(ConvertersProperty, value); }
    }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        foreach (var c in Converters)
            value = c.Convert(value, targetType, parameter, language);
        return value; 
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        foreach (var c in Converters)
            value = c.ConvertBack(value, targetType, parameter, language);
        return value; 
    }
}
于 2012-10-01T09:06:08.043 回答