Xamarin Forms: IMarkupExtension with bindable property does not work

后端 未结 1 786
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 02:06

The binding is not working for the Image tag. When I debug, I see that the value of the Source in Extension class is always null? But the content of the label is not null.

1条回答
  •  [愿得一人]
    2021-01-18 02:55

    Of course it does not !

    It's not because you inherit from BindableObject that magically your object has a BindingContext set. And without a BindingContext, there's no way to resolve the {Binding Image}.

    What you're looking for here is a Converter

    class ImageSourceConverter : IValueConverter
    {
        public object ConvertTo (object value, ...)
        {
            return ImageSource.FromResource(Source);
        }
    
        public object ConvertFrom (object value, ...)
        {
            throw new NotImplementedException ();
        }
    }
    

    You then add this converter to your Xaml root element resources (or Application.Resources and use it in your Bindings

    0 讨论(0)
提交回复
热议问题