C# IValueConverter中的可绑定属性始终为空

C# IValueConverter中的可绑定属性始终为空,c#,xamarin,xamarin.forms,ivalueconverter,bindableproperty,C#,Xamarin,Xamarin.forms,Ivalueconverter,Bindableproperty,我有一个转换器,它处理布尔值,并使用它们选择两个图像源中的任意一个。 我在转换器中将ImageSource定义为两个参数,稍后我需要使用XAML中的DynamicSource标记扩展提供这些资源,因此我设计了下面的代码 public class BooleanToImageSourceConverter : BindableObject, IValueConverter { public static readonly BindableProperty TrueImageSourcePr

我有一个转换器,它处理布尔值,并使用它们选择两个图像源中的任意一个。 我在转换器中将ImageSource定义为两个参数,稍后我需要使用XAML中的DynamicSource标记扩展提供这些资源,因此我设计了下面的代码

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


虽然我可以在property changed事件中看到每当调用convert方法时都会设置一个新值,但我可以看到两个图像源都为null。 我做错什么了吗?或者只是设计上不可能


请注意,由于应用程序的某些内部原因,我无法使用触发器来执行此操作

如果
null
返回,请向
value
添加测试。我们不清楚您的xaml是如何使用的。@Cfun xaml Addedh您是如何使用/使用转换器的?您只是在您所使用的xaml中创建它的一个实例added@Cfun非常标准的用法-只是添加了它-我不知道为什么它在
DynamicResource
中是空的,可能是其中之一。尝试使用正常工作的
StaticResource
,如果不能满足您的需要,您可以尝试重新设计,方法是避免使用
DynamicResource
,而改用
ConverterParameter
。有用的问题
<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}}" />