Forum Xamarin Xamarin.Forms

Can I know how to use ImageSource binding in Xamarin Forms?

Please guide me with an example as in how to use this binding imagesource. I am trying to a create a custom control with image and a label.

Best Answer

  • MabroukMabrouk USMember ✭✭✭
    Accepted Answer

    Hi, @AjayKumarKR

    1. You need creating an entity like :
      public class MyEntity { public string ImageSource {get;set;} public string LabelText {get;set;} }
    2. In your XAML.CS page you set your BindingContext like :
      public partial class YOUR_PAGE : ContentPage { public YOUR_PAGE() { InitializeComponent(); MyEntity myEntity=new MyEntity() { ImageSource="your_image.png"; //for example LabelText="your label text here"; }; BindingContext = myEntity; }

    3. in your XAML page, use binding like :
      <Label Text="{Binding LabelText}" /> <Image Source="{Binding ImageSource}" />

    Hope that helps ;)

    Mabrouk.

Answers

  • MabroukMabrouk USMember ✭✭✭
    Accepted Answer

    Hi, @AjayKumarKR

    1. You need creating an entity like :
      public class MyEntity { public string ImageSource {get;set;} public string LabelText {get;set;} }
    2. In your XAML.CS page you set your BindingContext like :
      public partial class YOUR_PAGE : ContentPage { public YOUR_PAGE() { InitializeComponent(); MyEntity myEntity=new MyEntity() { ImageSource="your_image.png"; //for example LabelText="your label text here"; }; BindingContext = myEntity; }

    3. in your XAML page, use binding like :
      <Label Text="{Binding LabelText}" /> <Image Source="{Binding ImageSource}" />

    Hope that helps ;)

    Mabrouk.

  • This works fine. But I am unable to figure out how to reference different images when creating different instances of the custom control.

    The example which i referred to was unclear. Could you please help with that and explain the related concepts?

    The example which I referred to is :
    http://blog.falafel.com/creating-reusable-xaml-user-controls-xamarin-forms/

    Please explain me the bindings in this code.

Sign In or Register to comment.