如何在Monotouch中绑定NSObject <eaaccessorydelegate>?</eaaccessorydelegate>

时间:2013-02-25 15:43:58

标签: c# ios objective-c xamarin.ios

我有以下Obj-C .h,绑定的正确方法是什么?

@interface iSmart : NSObject<EAAccessoryDelegate>{
  id<iSmartDelegate> delegate;
}

@property(nonatomic, assign) id<iSmartDelegate> delegate;
-(id)init;

@end

__________________________________________________________________________________________

@class iSmart;

@protocol iSmartDelegate <NSObject>

-(void) iSmartDidConnect;
-(void) iSmartDidDisconnect;
-(void) cardStatusChanged:(unsigned char)status;

@end

__________________________________________________________________________________________

在这一刻,我将其用于协议和界面:

[BaseType (typeof(NSObject))]
[Model]
interface iSmartDelegate
{
    [Export("iSmartDidConnect")]
    void iSmartDidConnect();

    [Export("iSmartDidDisconnect")]
    void iSmartDidDisconnect();

    [Export("cardStatusChanged:")]
    void CardStatusChanged(Byte status);

}


[BaseType (typeof (EAAccessoryDelegate), 
Delegates=new string [] { "WeakDelegate" },
Events=new Type [] { typeof (iSmartDelegate)})]
interface iSmart
{
    //@property(nonatomic, assign) id<iSmartDelegate> delegate;
    [Export("delegate"), NullAllowed]
    NSObject WeakDelegate { get; set; }

    [Wrap("WeakDelegate")]
    iSmartDelegate Delegate { get; set; }

    //-(id)init;        
    [Export("init")]
    NSObject init();
}

当我尝试在Xamarin Studio中构建项目时,我收到此错误   错误BI0000:意外错误 - 请在http://bugzilla.xamarin.com(BI0000)提交错误报告

由于

1 个答案:

答案 0 :(得分:4)

协议刚刚在您的ApiDefinition中内联,因此您实现了在iSmart定义中声明EAAccessoryDelegate的几个方法:

[BaseType (typeof(NSObject))]
interface iSmart : EAAccessoryDelegate{
    //bind the protocol here
    [Export ("accessoryDidDisconnect:")]
    void AccessoryDidDisconnect (EAAccessory accessory);
}

要绑定代理人,请查看http://docs.xamarin.com/guides/ios/advanced_topics/api_design#Delegates

[更新2013-02-26]你的委托绑定看起来没问题,除了应该编组到.NET unsigned char的本地byte,因为.NET char类型是2字节长,以适应unicode字符。

[更新2013-02-27]另外,正如您最近在问题中添加的那样,绑定构造函数的正确方法是这样的(参见http://docs.xamarin.com/guides/ios/advanced_topics/binding_objective-c_libraries中的3.3):

[Export ("init")]
IntPtr Constructor ();