How to bind NSObject in Monot

2019-07-22 03:54发布

问题:

I have the following Obj-C .h, what is the correct way to bind?

@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

__________________________________________________________________________________________

In this moment I have this for the protocol and interface:

[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();
}

I'm getting this error when i try to build the project in Xamarin Studio Error BI0000: Unexpected error - Please file a bug report at http://bugzilla.xamarin.com (BI0000)

Thanks

回答1:

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 ();