点击uiimageview上的事件ios Xamarin C#

时间:2014-09-04 08:35:49

标签: c# ios uiimageview xamarin uigesturerecognizer

如何在Xamarin for iOS中获取click UIImageView事件?

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");
}

我是否需要使用手势点击UIimageviewclick是否有UIImageView个事件?

在Titanium中,click的{​​{1}}事件为UIImageView。 请告诉我Xamarin中有任何actionListner事件吗?

2 个答案:

答案 0 :(得分:8)

从技术上讲,Xamarin.iOS中的UIImageView没有Click事件,因为iOS SDK中没有UIImageView的Click事件。 Xamarin将iOS SDK概念直接映射到C#构造,因此您在C#中看到的就是您在Objective-C中看到的(大多数情况下)。您在Titanium中看到的是他们自己的抽象和功能被添加到他们的特定框架之上,并且除了实际的iOS SDK之外。

话虽如此,对你来说是一个可行的选择(因为你看起来想要使用UIImageView作为一个按钮,就是简单地创建一个看起来像Image而不是相反的按钮。这样的东西应该有效

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.
     };
}

答案 1 :(得分:3)

试试这个为我工作

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