我正在尝试将用 Swift 编写的框架绑定(bind)到我的 Xamarin 应用程序。该框架有一个具有以下方法的委托(delegate):
onError(error: IError)
IError 是 Swift 中的一个协议(protocol)(4):
@objc public protocol IError {
var message: String { get }
}
它的绑定(bind)由 Sharpie 生成并由我修改,如下所示:
// @protocol IError
[BaseType(typeof(NSObject))] // <- Added by me. Won't compile without it.
[Protocol, Model]
abstract class IError
{
// @required @property (readonly, copy, nonatomic) NSString * _Nonnull message;
[Abstract]
[Export("message")]
string Message { get; }
}
当调用回调时,我的应用程序中出现以下错误:
System.MemberAccessException has been thrown
Cannot create an instance of IError because it is an abstract class
为什么我的应用程序尝试实例化 IError?框架不应该这样做吗?它与[BaseType(typeof(NSObject))]
有关吗?
最佳答案
终于找到解决方案here 。您需要在绑定(bind)中声明一个具有相同名称但以“I”开头的裸接口(interface),然后它会神奇地为您收到的接口(interface)而不是真实对象创建一个包装器。
绑定(bind)现在看起来像这样:
//bare interface
interface IIError {}
// @protocol IError
[BaseType(typeof(NSObject))] // <- Added by me. Won't compile without
it.
[Protocol, Model]
abstract class IError
{
// @required @property (readonly, copy, nonatomic) NSString *
_Nonnull message;
[Abstract]
[Export("message")]
string Message { get; }
}
该方法的绑定(bind)如下所示:
[Abstract]
[Export("onError:")]
void OnError(IIError error);
编辑: 似乎也有记录here 。不知何故错过了。
关于框架回调传递接口(interface)时出现 Xamarin 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49475858/