框架回调传递接口(interface)时出现 Xamarin 异常

标签 xamarin xamarin.ios

我正在尝试将用 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/

相关文章:

azure - Visual Studio 2017 版本 15.3.2 缺少 Azure 移动应用程序模板

xamarin - 配对 mac 错误无法执行 'grep "ssh-rsa ...."

c# - 将 AVAudioRecorder.ToUrl() 与 MonoTouch 4.0.1 一起使用返回 null

ios - Xamarin 上的 NSItemProviderReading

uitableview - MvvmCross TableView 绑定(bind)中的 "System.InvalidOperationException: Collection was modified"

VS for Mac 2019 上的 Xamarin WatchOS 构建错误

c# - 我是否必须在 MonoTouch 的 UITableViewSource 中保留对 GetCell() 和 GetViewForHeader() 的返回值的显式引用?

android - 罢工 html 标签不呈现在 EditText 与 TextFormatted

xamarin - 徽章计数未显示在 Xamarin Forms 中

wcf - Visual Studio 2015 + Xamarin + native 可移植 PCL + WCF