我的应用程序中有以下2个协议:
POST
我使用Objective Sharpie绑定它们,我得到以下绑定:
TRACE
这看起来很合理。除非我尝试实施OPTION
,否则@protocol DisplayItem<NSObject>
- (void) itemSelected:(NSString*) itemId;
@end
@protocol DisplayerFactory <NSObject>
- (UIViewController<DisplayItem>*) createItemDisplayer;
@end
方法需要返回// @protocol DisplayItem <NSObject>
[Protocol, Model]
[BaseType(typeof(NSObject))]
interface DisplayItem
{
// @required -(void)itemSelected:(NSString *)itemId;
[Abstract]
[Export("itemSelected:")]
void ItemSelected(string itemId);
}
// @protocol DisplayerFactory <NSObject>
[Protocol, Model]
[BaseType(typeof(NSObject))]
interface DisplayerFactory
{
// @required -(UIViewController<DisplayItem> *)createItemDisplayer;
[Abstract]
[Export("createItemDisplayer")]
DisplayItem CreateItemDisplayer();
}
和DisplayerFactory
。由于CreateItemDisplayer
是作为一个类实现的,我们不能在C#中进行多重继承,因此我不能拥有一个继承自DisplayItem
和UIViewController
的类。
我尝试使用DisplayItem
,如下所示:
UIViewController
但由于DisplayItem
不是IDisplayItem
,因此无法编译。如果我将方法类型更改为public class MyController : UIViewController, IDisplayItem
{
// ...
}
public DisplayItem CreateItemDisplayer()
{
return new MyController();
}
,则无法编译,因为我没有正确实现MyController
接口(或覆盖DisplayerFactory)。
如何解决此问题,以便我可以返回实现协议的DisplayItem
。
答案 0 :(得分:4)
这样绑定:
interface IDisplayItem {}
// @protocol DisplayerFactory <NSObject>
[Protocol, Model]
[BaseType(typeof(NSObject))]
interface DisplayerFactory
{
// @required -(UIViewController<DisplayItem> *)createItemDisplayer;
[Abstract]
[Export("createItemDisplayer")]
IDisplayItem CreateItemDisplayer();
}
现在这段代码可以正常工作,因为CreateItemDisplayer
会返回一个接口:
public class MyController : UIViewController, IDisplayItem
{
// ...
}
public IDisplayItem CreateItemDisplayer()
{
return new MyController();
}