1

How to get click event of UIImageView in Xamarin for iOS?

public UIImageView img_UploadImage { get; set;}

public ConstructorClasss(){
img_UploadImage = new UIImageView();
img_UploadImage.Frame = new RectangleF(100,100,60,50);
img_UploadImage.Image = UIImage.FileName ("UploadLocal.png");
}

Do I need to use gestures tap for UIimageview or is there any click event for UIImageView?

In Titanium there is a click event for UIImageView with actionListner. Please let me know is there any click event in Xamarin?

Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
kiran
  • 4,285
  • 7
  • 53
  • 98
  • http://stackoverflow.com/q/3775577/3507404 – Ingweland Sep 04 '14 at 10:03
  • Yes, technically speaking there is a GestureRecognizers collection on all UIView classes that you can add getsture support to. There is no "Click" associated with a UIImageView. – jensendp Sep 08 '14 at 17:42

2 Answers2

8

Technically speaking there is no Click event for a UIImageView in Xamarin.iOS because there is no Click event for UIImageView in the iOS SDK. Xamarin maps the iOS SDK concepts directly to C# constructs so what you see in C# is what you see in Objective-C (for the most part). What you are seeing in Titanium is their own abstraction and functionality being added to their particular framework on top of and in addition to the actual iOS SDK.

Having said that, a viable option for you (since it looks like you want to use the UIImageView as a button, is to simply create a button that looks like an Image instead of the other way around. Something like this should work.

public UIButton img_UploadImage { get; set; }

public ConstructorClass(){
     img_UploadImage = UIButton.FromType(UIButtonType.Custom);
     img_UploadImage.Frame = new RectangleF(100, 100, 60, 50);
     img_UploadImage.setImage(UIImage.FromFile("UploadLocal.png");

     //Set up event handler for "Click" event ("TouchUpInside in iOS terminology)
     img_UploadImage.TouchUpInside += (object sender, EventArgs e) => {
          //Do some action.
     };
}
jensendp
  • 2,135
  • 15
  • 15
  • 2
    No problem. It doesn't bother me. I was curious if I was wrong about something. If I submit answers that are incorrect I like to be corrected. It's the best way to learn. – jensendp Sep 08 '14 at 20:15
4

try this working for me

private void AddTapGesture()
{
    //  you can achieve this from user interface look image below
    YourImageView.UserInteractionEnabled = true;
    var tapGesture = new UITapGestureRecognizer(this,
        new ObjCRuntime.Selector("ImageTrigger:"))
    {
        NumberOfTapsRequired = 1 // change number as you want 
    };
    YourImageView.AddGestureRecognizer(tapGesture);
}

[Export("ImageTrigger:")]
public void ImageTrigger(UIGestureRecognizer sender)
{
    System.Diagnostics.Debug.WriteLine("Button Clicked");
}

enter image description here

DavidG
  • 113,891
  • 12
  • 217
  • 223
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156