2

I have a converter that deals with Boolean values and uses them to pick either of two ImageSources. I defined the ImageSource as two parameters in the converter, later on I need to supply those resources using the DynamicRsource markup extension from the XAML so I devised the code below

public class BooleanToImageSourceConverter : BindableObject, IValueConverter
{
    public static readonly BindableProperty TrueImageSourceProperty = BindableProperty.Create(nameof(TrueImageSource), typeof(ImageSource), typeof(BooleanToImageSourceConverter));
    public static readonly BindableProperty FalseImageSourceProperty = BindableProperty.Create(nameof(FalseImageSource), typeof(ImageSource), typeof(BooleanToImageSourceConverter), propertyChanged: Test);

    private static void Test(BindableObject bindable, object oldValue, object newValue)
    {
        if (oldValue == newValue)
            return;

        var control = (BooleanToImageSourceConverter)bindable;
    }

    public ImageSource TrueImageSource
    {
        get => (ImageSource)GetValue(TrueImageSourceProperty);
        set => SetValue(TrueImageSourceProperty, value);
    }
    public ImageSource FalseImageSource
    {
        get => (ImageSource)GetValue(FalseImageSourceProperty);
        set => SetValue(FalseImageSourceProperty, value);
    }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool isTrue = (bool) value;
        return isTrue ? TrueImageSource : FalseImageSource;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value == TrueImageSource;
    }
}

XAML

<converters:BooleanToImageSourceConverter
    x:Key="FavImageSourceConverter"
    FalseImageSource="{DynamicResource savedToFav}"
    TrueImageSource="{DynamicResource saveToFav}" />

<ImageButton
    BackgroundColor="Transparent"
    Command="{Binding SetFavCommand}"
    HorizontalOptions="End"
    Source="{Binding IsFavorite, Converter={StaticResource FavImageSourceConverter}}" />

although I can see in the property changed event that a new value is being set whenever the convert method is called I can see that both image sources are null. Am I doing something wrong here? or is it just not possible by design?

Please note that I can not use triggers to do this because of some internal reasons to the application

8
  • Add a test to value if null return Binding.DoNothing. It is not clear for us how is your xaml though.
    – Cfun
    Commented Nov 18, 2020 at 15:15
  • @Cfun XAML added
    – Scarnet
    Commented Nov 18, 2020 at 15:42
  • 1
    How are you using/consuming your converter? You are only creating an instance of it in the xaml you added
    – Cfun
    Commented Nov 18, 2020 at 16:24
  • @Cfun pretty standard usage -just added it-
    – Scarnet
    Commented Nov 19, 2020 at 10:47
  • I don't know why it is null with DynamicResource, probably one of the multiple DynamicResource issues on the platform. Tried with StaticResource working properly, If i t does not fir your needs you can try a redesign by avoiding using DynamicResource and using ConverterParameter instead. helpful question stackoverflow.com/q/11323169/5228202
    – Cfun
    Commented Nov 19, 2020 at 18:22

0