-->

Monotouch Binding Syntax For Protocols

2020-07-20 07:56发布

问题:

If I have the following ( a protocol and then an interface which uses that protocol), what is the correct way to set up the ApiDefinition for btouch? I've got most of the .h file converted but this one is tricking me up.

Thanks

Jeff

@protocol GRGrabbaPreferencesProtocol <NSObject>
- (NSString*) baseNamepace; 
@end 

@interface GRGrabbaPreferences : NSObject <GRGrabbaPreferencesProtocol>
{ 
    GRGrabbaBarcodePrefs   *barcode; 
} 
@property (retain) GRGrabbaBarcodePrefs   *barcode; 
@end 

@interface GRGrabbaBarcodePrefs    : NSObject <GRGrabbaPreferencesProtocol>
@end 

回答1:

Protocols are really just inlined into your interface, so you can either merely inline the properties directly into your class, or you can have the generator inline those for you.

// Notice the lack of [BaseType] attribute on this one
interface GRGrabbaPreferencesProtocol {
    [Export ("baseName")]
    string BaseName { get; }
}

[BaseType (typeof (NSObject))]
interface GRGrabbaPreferences : GRGrabbaPreferencesProtocol {
    [Export ("barcode")]
    GRGrabbaBarcodePrefs Barcode { get; }
}

[BaseType (typeof (NSObject))]
interface GRGrabbaBarcodePrefs : GRGrabbaPreferencesProtocol {
}

The above is identical to:

[BaseType (typeof (NSObject))]
interface GRGrabbaPreferences : GRGrabbaPreferencesProtocol {
    [Export ("baseName")]
    string BaseName { get; }

    [Export ("barcode")]
    GRGrabbaBarcodePrefs Barcode { get; }
}

[BaseType (typeof (NSObject))]
interface GRGrabbaBarcodePrefs : GRGrabbaPreferencesProtocol {
    [Export ("baseName")]
    string BaseName { get; }
}

It is more practical to let the generator take over the inlining to avoid errors and cut/paste problems. But notice that no GRGrabbaPreferencesProtocol is exported to C# in any form.