目的Sharpie笔有时会添加我对@protocols
问题描述:
我试图绑定一个库,我遇到了这个问题:目的Sharpie笔有时会添加我对@protocols
// @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从继承的接口列表中删除?或者我可以在不更改这些的情况下修复此问题,只需向这些类/接口添加或删除属性即可?
答
从MonoTouch 7.0开始,新的和改进的协议绑定功能已被合并。包含[协议]属性的任何定义,实际上会产生三个支持类,大大提高你消耗协议的方式:
// 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定义中编写空的接口。如果你想使用MyProtocol的API中,你需要做的是:
[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();
}
不是重复:http://stackoverflow.com/questions/36448006/sharpie -binding-objective-c-protocols-issue这并不回答我的问题。它告诉我可以删除I,但我想深入了解Sharpie生成的目标以及我应该如何解决此问题。 – vrwim
在https://developer.xamarin.com/guides/cross-platform/macios/binding/binding-types-reference/#Protocols中找到答案 – vrwim