MonoTouch 綁定

對綁定不瞭解的,請參考這篇文章 http://www.cnblogs.com/beginor/archive/2012/04/22/2465441.htmlhtml

本文主要講述上文沒有提到的ide

1.構造函數函數

在MonoTouch綁定中,構造函數名是固定的,都是 Constructor,返回類型:IntPtr.this

Obj-C 代碼:atom

-(id)initWithCenterViewController:(UIViewController *)centerViewController leftDrawerViewController:(UIViewController *)leftDrawerViewController rightDrawerViewController:(UIViewController *)rightDrawerViewController;

綁定到MonoTouch:spa

[Export ("initWithCenterViewController:leftDrawerViewController:rightDrawerViewController:")]
IntPtr Constructor (UIViewController centerViewController, UIViewController leftDrawerViewController, UIViewController rightDrawerViewController);        

2.代碼塊code

在Obj-C中,代碼塊很經常使用.它和.NET的Lamda表達式類似.能夠綁定到委託(Delegate).htm

Obj-C代碼:blog

 -(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 開發團隊.

相關文章
相關標籤/搜索