对绑定不了解的,请参考这篇文章 http://www.cnblogs.com/beginor/archive/2012/04/22/2465441.html
本文主要讲述上文没有提到的
1.构造函数
在MonoTouch绑定中,构造函数名是固定的,都是 Constructor,返回类型:IntPtr.
Obj-C 代码:
-(id)initWithCenterViewController:(UIViewController *)centerViewController leftDrawerViewController:(UIViewController *)leftDrawerViewController rightDrawerViewController:(UIViewController *)rightDrawerViewController;
绑定到MonoTouch:
[Export ("initWithCenterViewController:leftDrawerViewController:rightDrawerViewController:")] IntPtr Constructor (UIViewController centerViewController, UIViewController leftDrawerViewController, UIViewController rightDrawerViewController);
2.代码块
在Obj-C中,代码块很常用.它和.NET的Lamda表达式相似.可以绑定到委托(Delegate).
Obj-C代码:
-(void)setDrawerVisualStateBlock:(void(^)(MMDrawerController * drawerController, MMDrawerSide drawerSide, CGFloat percentVisible))drawerVisualStateBlock;
绑定到MonoTouch时,分两部分:
delegate void SetDrawerVisualStateBlockCallback (MMDrawerController drawerController, MMDrawerSide drawerSide, float percentVisible);
[Export ("setDrawerVisualStateBlock:")] void setDrawerVisualStateBlock (SetDrawerVisualStateBlockCallback drawerVisualStateBlock);
在实际调用时:
this.drawerController.setDrawerVisualStateBlock ((MMDrawerController drawerController, MMDrawerSide drawerSide, float percentVisible)=>{ Console.WriteLine("setDrawerVisualStateBlock"); });
3.委托(Delegate)
这个比较直接,和.NET相似。
Obj-C代码:
@protocol FPPopoverControllerDelegate <NSObject> @optional - (void)popoverControllerDidDismissPopover:(FPPopoverController *)popoverController; - (void)presentedNewPopoverController:(FPPopoverController *)newPopoverController shouldDismissVisiblePopover:(FPPopoverController*)visiblePopoverController; @end
@property(nonatomic,assign) id<FPPopoverControllerDelegate> delegate;
绑定到MonoTouch时,也分两部分:
[Model, BaseType (typeof(NSObject))] interface FPPopoverControllerDelegate { [Export ("popoverControllerDidDismissPopover:")] void popoverControllerDidDismissPopover (FPPopoverController popoverController); [Export ("shouldDismissVisiblePopover:visiblePopoverController")] void shouldDismissVisiblePopover (FPPopoverController newPopoverController, FPPopoverController visiblePopoverController); }
[Export("delegate")] NSObject WeakDelegate { get; set; } [Wrap("WeakDelegate")] FPPopoverControllerDelegate Delegate { get; set; }
4.绑定类别(Category)
这个似乎目前还没有实现.预计它以后的实现可能是这样:
namespace XYOrigami { delegate void OrigamiAnimationCompleted (bool finished); [BaseType (typeof(UIView))] [Category] interface Origami{ [Export ("showOrigamiTransitionWith:NumberOfFolds:Duration:Direction:completion:")] void ShowOrigamiTransition (UIView view, int folds, float duration, XYOrigamiDirection direction, OrigamiAnimationCompleted completed); [Export ("hideOrigamiTransitionWith:NumberOfFolds:Duration:Direction:completion:")] void HideOrigamiTransition (UIView view, int folds, float duration, XYOrigamiDirection direction, OrigamiAnimationCompleted completed); } }
以上推测的依据是从 http://stackoverflow.com/questions/14961716/monotouch-binding-categories-using-category-attribute 而来.回答者 chrisntr 属于Xamarin 开发团队.