IOS到C#相当于id<>

时间:2017-05-02 08:14:21

标签: c# ios xamarin

我有一个IOS代码要在c#中重写这个id<UIGestureRecognizerDelegate> gestureDelegate;

我怎样才能在c#中重写它?

@property (weak, nonatomic) id<UIGestureRecognizerDelegate> gestureDelegate;

其实我声明它像UIGestureRecognizerDelegate gestureDelegate;但是它是对的吗? 如果我理解正确,id就像C#中的var一样? 如果是这样我怎么能将它声明为类参数?

1 个答案:

答案 0 :(得分:4)

C#interface与Objective-C中的protocol类似,id<UIGestureRecognizerDelegate>表示该对象实现UIGestureRecognizerDelegate中定义的方法。因此,C#中的相同内容可能是(主要思想,未在IDE中测试):

interface UIGestureRecognizerDelegate {
   public bool gestureRecognizerShouldBegin(UIGestureRecognizer gestureRecognizer);
}

class Example {
   public UIGestureRecognizerDelegate gestureDelegate; //the equivalent
}

符合界面的类:

public class ExampleViewController:UIViewController,UIGestureRecognizerDelegate {
   var example = Example();

   public ExampleViewController() {
       this.example.gestureDelegate = this;
   }

   //implements the methods of interface UIGestureRecognizerDelegate
   public bool gestureRecognizerShouldBegin(UIGestureRecognizer gestureRecognizer) {
       //......
   }
}