【问题标题】:Objective Sharpie sometimes adds I to @protocolsObjective Sharpie 有时将 I 添加到 @protocols
【发布时间】:2016-10-27 09:27:37
【问题描述】:

我正在尝试绑定一个库,我遇到了这个问题:

// @interface PTPusher : NSObject <PTPusherConnectionDelegate, PTPusherEventBindings>
[BaseType(typeof(NSObject))]
interface PTPusher : IPTPusherConnectionDelegate, IPTPusherEventBindings

找不到IPTPusherConnectionDelegate 和IPTPusherEventBindings,但它们未更改的名称确实存在:

// @protocol PTPusherConnectionDelegate <NSObject>
[Protocol, Model]
[BaseType(typeof(NSObject))]
interface PTPusherConnectionDelegate

为什么Objective Sharpie 在继承的接口列表中添加I,而不是在接口名称本身?

我应该改变什么来解决这个问题?我是在接口名称中添加 I 还是从继承的接口列表中删除 I?或者我可以在不更改这些内容的情况下解决这个问题,只需向这些类/接口添加或删除属性?

【问题讨论】:

标签: xamarin xamarin.ios objective-sharpie xamarin-binding


【解决方案1】:

从 MonoTouch 7.0 开始,一个新的和改进的协议绑定功能被合并。任何包含 [Protocol] 属性的定义实际上都会生成三个支持类,它们极大地改进了您使用协议的方式:

// Full method implementation, contains all methods
class MyProtocol : IMyProtocol {
    public void Say (string msg);
    public void Listen (string msg);
}

// Interface that contains only the required methods
interface IMyProtocol: INativeObject, IDisposable {
    [Export ("say:”)]
    void Say (string msg);
}

// Extension methods
static class IMyProtocol_Extensions {
    public static void Optional (this IMyProtocol this, string msg);
    }
}

还有,

如果您想在 API 中使用协议定义,则需要在 API 定义中编写骨架空接口。如果您想在 API 中使用 MyProtocol,您需要这样做:

[BaseType (typeof (NSObject))]
[Model, Protocol]
interface MyProtocol {
    // Use [Abstract] when the method is defined in the @required section
    // of the protocol definition in Objective-C
    [Abstract]
    [Export ("say:")]
    void Say (string msg);

    [Export ("listen")]
    void Listen ();
}

interface IMyProtocol {}

[BaseType (typeof(NSObject))]
interface MyTool {
    [Export ("getProtocol")]
    IMyProtocol GetProtocol ();
}

来源:https://developer.xamarin.com/guides/cross-platform/macios/binding/binding-types-reference/#Protocols

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多