基于Xamarin中的属性值的图像源代码

问题描述:

我有一个自定义的MyCachedImage继承自FFImageLoading.Forms.CachedImage,它在ListView中用于显示图像。 此图像的来源由2个属性组成:自定义对象作为实体和作为大小的整数。 比方说,如果实体是“城市”对象,大小为10,则图像源将只有当两个属性增值的“http://..../city/10/image.png基于Xamarin中的属性值的图像源代码

图片来源必须设置好的。

所以,我的答案是,如何以及何时创建源网址?

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); 
    } 
}