2

我有以下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)提交错误报告

谢谢

4

1 回答 1

4

Protocols are just inlined in your ApiDefinition, so you implement declare the few methods of EAAccessoryDelegate in your iSmart definition:

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

For binding the delegate, look at http://docs.xamarin.com/guides/ios/advanced_topics/api_design#Delegates

[UPDATE 2013-02-26] your delegate binding looks ok, except for the native unsigned char that should be marshalled to .NET byte as the .NET char type is 2 bytes long to fit unicode characters.

[UPDATE 2013-02-27] Also, as you lately added that to your question, the proper way to bind a constructor is like this (see 3.3 in http://docs.xamarin.com/guides/ios/advanced_topics/binding_objective-c_libraries) :

[Export ("init")]
IntPtr Constructor ();
于 2013-02-25T16:43:58.053 回答