markg
Topic Author
Posts: 2
Joined: 05 Mar 2015, 13:01

Value Converter Group crashes build tool

13 Mar 2015, 14:57

I am trying to integrate a value converter group so that I can invert a bool and then convert the result to a visibility. When I add the following to my xaml the build tool crashes Unity at startup:
<common:ValueConverterGroup x:Key="invertBoolAndChangeVisibilityConverter">
        <common:InvertBoolConverter/>
        <common:BoolToVisibilityConverter/>
      </common:ValueConverterGroup>
The code for the converter:
public class ValueConverterGroup : List<IValueConverter>, IValueConverter
	{
		public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			return this.Aggregate (value, (current, converter) => converter.Convert (current, targetType, parameter, culture));
		}
	
		public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException ();
		}
	}
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: Value Converter Group crashes build tool

16 Mar 2015, 21:37

The crash comes from using a converter (IValueConverter) that also implements another interface (IList).
We don't support objects implementing multiple interfaces, but we should warn the user without crashing this way, we will fix this.

You can achieve the same result by using a property to contain children converters:
[System.Windows.Markup.ContentProperty("Children")]
public class ValueConverterGroup : IValueConverter
{
    public List<IValueConverter> Children { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        object current = value;
        foreach (var converter in Children)
        {
            current = converter.Convert(current, targetType, parameter, culture);
        }
        return current;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
} 

Who is online

Users browsing this forum: No registered users and 0 guests