1

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?

IWillByte
  • 33
  • 4

2 Answers2

0

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!");
           };
      }
}
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
0

Thanks to Rohit's answer, I was able to create a custom control that I modified to take an anonymous function as a parameter. Here is the code:

class ClickableImage : Image
{
    private TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer();

    public ClickableImage(Action action)
    {
        tapGestureRecognizer.Tapped += (s, e) => 
        {
           System.Diagnostics.Debug.WriteLine("Image Clicked w/ Lambda");
           action();
        };

        GestureRecognizers.Add(tapGestureRecognizer);
    }
}
Community
  • 1
  • 1
IWillByte
  • 33
  • 4