我在我的一个班级上设置了 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