我在我的一个类上设置了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
答案 0 :(得分: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;
}
}