SetBinding for Embedded Resource in ItemTemplate

M0JIM0JI GBMember

I have a list view in Xamarin.Forms and I have set ItemTemplate e.g.

listView.ItemTemplate = new DataTemplate(() =>
{
  Image img = new Image();
  img.SetBinding(Image.SourceProperty, "ImageUrl");
  ...so on
}

This works if image is stored in the same project but I have moved image to PCL project and set to "Embedded Resource".

I need to state somehting like below but it doesn't work, how can I achieve this so it will bind "embedded resource" to image control in list item Template.

img.SetBinding(Image.SourceProperty, "ImageSource.FromResource(ImageUrl)");   // won't work

Thanks

Best Answer

Answers

  • M0JIM0JI GBMember

    any example bit of code?

    I cannot do
    img.SetBinding(Image.SourceProperty, ImageSource.FromResource("ImageUrl"));

    second expected param is a string type

  • kentuckerkentucker USMember ✭✭✭✭✭

    Try something like this. And bind to the new property.

        public ImageSource MyImage
        {
            get
            {
                return ImageSource.FromResource("MyApp.Images.MyImage.png");
            }
        }
    

    You might even be able to do this.

        img.Source = ImageSource.FromResource("ImageUrl");
    
  • M0JIM0JI GBMember
    edited January 2018

    Kentucker that only works if my image was fixed, always the same but "ImageUrl" is different for each list item :neutral:

    I think one way may be if I include extra ImageSource property in my model and bind to that instead but I was trying to avoid extra ImageSource in binding model. we are talking inside a DataTemplate for listview

    would be great if there was some magic for setting imgage souce binding to Embedded Resource in DataTemplate from ImageUrl member

  • M0JIM0JI GBMember

    Thanks for your suggestion, I initially thought readonly property on the view itself but as you prob meant I created readonly property in model to return ImageSource and it still uses another property ImageUrl to return whatever image so I have it's working nicely :)

    e.g.
    listView.ItemTemplate = new DataTemplate(() =>
    {
    Image img = new Image();
    img.SetBinding(Image.SourceProperty, "ImageUrl");
    img.SetBinding(Image.SourceProperty, "ImageUrlSource"); // ImageUrlSource returns ImageSource based on property ImageUrl
    ...so on
    }

  • NesrinNesrin Member

    @M0JI said:
    Thanks for your suggestion, I initially thought readonly property on the view itself but as you prob meant I created readonly property in model to return ImageSource and it still uses another property ImageUrl to return whatever image so I have it's working nicely :)

    e.g.
    listView.ItemTemplate = new DataTemplate(() =>
    {
    Image img = new Image();
    img.SetBinding(Image.SourceProperty, "ImageUrl");
    img.SetBinding(Image.SourceProperty, "ImageUrlSource"); // ImageUrlSource returns ImageSource based on property ImageUrl
    ...so on
    }

    Can you please share the code of how you implemented // ImageUrlSource returns ImageSource based on property ImageUrl please

Sign In or Register to comment.