Image source based on properties value

MirkoLongaMirkoLonga USMember ✭✭

Hi guys,

I've got a custom MyCachedImage that inherits from FFImageLoading.Forms.CachedImage, which is used in a ListView to display images.
The source of this image should be composed by 2 properties: string entity, int size.
Let's say if entity is "city" and size is 10 then the image source will be "http://..../city/10/image.png"

The entity property is then passed as parameter to a command via a gesture, so I need it as property.

Source must be setted only when both properties are valorized.

So, my answer is, how and when create the source url?

MyCachedImage.vb

public class MyCachedImage : CachedImage
{
    public static readonly BindableProperty EntityProperty =
        BindableProperty.Create(nameof(Entity), typeof(string), typeof(MyCachedImage));
    public string Entity
    {
        get { return (string)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

    < control:MyCachedImage Size="10"
                         Entity="city"
                         WidthRequest="40"
                         HeightRequest="40" />

Best Answer

  • MirkoLongaMirkoLonga US ✭✭
    Accepted Answer

    Hi Nhilesh , thanks.
    I was wondering on when create that string and I found the right solution.
    The OnBindingContextChanged is called when all properties are setted, so:

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

Answers

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @MirkoLonga

    If I understand it correctly,

    You would like to do something on these lines.

    public string ImagePath
    {
    get { return string.Format("http://..../city/{0} {1}", Entity, Size); }
    set { //Do Nothing }
    }

    Hope this helps,

    Regards,
    N Baua

  • MirkoLongaMirkoLonga USMember ✭✭
    Accepted Answer

    Hi Nhilesh , thanks.
    I was wondering on when create that string and I found the right solution.
    The OnBindingContextChanged is called when all properties are setted, so:

        protected override void OnBindingContextChanged()
        {
            base.OnBindingContextChanged();
            if (_source == string.Empty)
            {
                Source = Helpers.ImageHelper.UriFromEntity(Entity, ImageSize);
            }
        }
    
Sign In or Register to comment.