Properties 基于Xamarin.Forms中属性值的图像源

Properties 基于Xamarin.Forms中属性值的图像源,properties,xamarin.forms,ffimageloading,Properties,Xamarin.forms,Ffimageloading,我有一个自定义的MyCachedImage,它继承自FFImageLoading.Forms.CachedImage,在ListView中用于显示图像。 此图像的源由两个属性组成:一个自定义对象作为实体,一个整数作为大小。 假设实体为“城市”对象,大小为10,则图像源为“” 只有当两个属性都被定值时,才必须设置图像源。 所以,我的答案是,如何以及何时创建源url MyCachedImage.vb public class MyCachedImage : CachedImage { pub

我有一个自定义的MyCachedImage,它继承自FFImageLoading.Forms.CachedImage,在ListView中用于显示图像。 此图像的源由两个属性组成:一个自定义对象作为实体,一个整数作为大小。 假设实体为“城市”对象,大小为10,则图像源为“”

只有当两个属性都被定值时,才必须设置图像源。

所以,我的答案是,如何以及何时创建源url

MyCachedImage.vb

public class MyCachedImage : CachedImage
{
    public static readonly BindableProperty EntityProperty =
        BindableProperty.Create(nameof(Entity), typeof(MyObject), typeof(MyCachedImage));
    public MyObject Entity
    {
        get { return (MyObject)GetValue(EntityProperty); }
        set { SetValue(EntityProperty, value); }
    }

    public static readonly BindableProperty SizeProperty =
        BindableProperty.Create(nameof(Size), typeof(int), typeof(MyCachedImage), defaultValue: 0);
    public int Size
    {
        get { return (int)GetValue(SizeProperty); }
        set { SetValue(SizeProperty, value); }
    }

   public MyCachedImage()
    {
    ???  set source here?
   }
   protected override void OnBindingContextChanged()
    {
    ???  set source here?
    }
}
MyPage.xaml

<ListView ....>
....
<control:MyCachedImage Size="10"
                     Entity="{Binding MyObject}"
                     WidthRequest="40"
                     HeightRequest="40" />
....
</ListView>

....
....

我想知道何时创建该字符串,我找到了正确的解决方案。 设置所有属性时调用OnBindingContextChanged,因此:

protected override void OnBindingContextChanged()
{
    base.OnBindingContextChanged();
    if (_source == string.Empty)
    {
        Source = Helpers.ImageHelper.UriFromEntity(Entity, ImageSize);
    }
}