IWillByte IWillByte - 3 months ago 47
Android Question

Xamarin.Forms - How do I make a custom view with a clickable image?

I am making an app that has a grid of many images that need to handle click events. I am able to make a single image clickable with this,

Image clickableImage = new Image();
clickableImage.Source = ImageSource.FromFile("image.png");

imageTap.Tapped += (object sender, EventArgs e) =>
{
System.Diagnostics.Debug.WriteLine("Image clicked!");
};

clickableImage.GestureRecognizers.Add(imageTap);


but I need to have >20 clickable images. Is there a way to create something like an Angular2 template?

Answer

You can create a Custom Control in your shared project itself which inherits from image and supports click and use it through out your application.

ClickableImage : Image 
{
      public ClickableImage()
      {
           Tapped += (sender, e) => 
           {
                 System.Diagnostics.Debug.WriteLine("Image clicked!");
           };
      }
}
Comments