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

Cfun
  • 8,442
  • 4
  • 30
  • 62
Scarnet
  • 738
  • 2
  • 11
  • 36
  • Add a test to `value` if `null` return [Binding.DoNothing](https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.binding.donothing?view=xamarin-forms). It is not clear for us how is your xaml though. – Cfun Nov 18 '20 at 15:15
  • @Cfun XAML added – Scarnet Nov 18 '20 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 Nov 18 '20 at 16:24
  • @Cfun pretty standard usage -just added it- – Scarnet Nov 19 '20 at 10:47
  • I don't know why it is null with `DynamicResource`, probably one of the [multiple `DynamicResource` issues on the platform](https://github.com/xamarin/Xamarin.Forms/issues?q=is%3Aissue+is%3Aopen+DynamicResource). 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 https://stackoverflow.com/q/11323169/5228202 – Cfun Nov 19 '20 at 18:22
  • @Cfun unfortunately neither StaticResource nor ConverterParameter would do it for me, I ended up creating a separate converter just for this specific business case, so I don't have to set the values for the properties, which is a design smell, but would could you do – Scarnet Nov 21 '20 at 14:52
  • Nice, you could share your solution as an answer for others having the same problem – Cfun Nov 21 '20 at 15:17
  • Any solution update? – Cfun Feb 01 '21 at 18:52

0 Answers0