gpt4 book ai didi

objective-c - Xamarin 将 objective-c 库绑定(bind)到 C# 委托(delegate)和事件

转载 作者:太空狗 更新时间:2023-10-30 03:51:12 25 4
gpt4 key购买 nike

这是我上次 question 的跟进关于将 objective-c 库绑定(bind)到 xamarin 项目。所以我想我会自己创建 api 定义,但我不知道如何将 objective-c 委托(delegate)重写为 C# 委托(delegate)/事件。我开始理解 objective-c 委托(delegate)与 C# 委托(delegate)不同,而更像是 C# 事件。

这是 objective-c 委托(delegate)定义(来自头文件):

@protocol LineaDelegate
@optional

-(void)connectionState:(int)state;

@end

这是 objective-c 类定义(来自头文件):

@interface Linea : NSObject

-(void)connect;

@end

connect 方法在后台工作,并通过 connectionState 委托(delegate)通知调用者连接成功。

现在,我该如何正确绑定(bind) api?到目前为止,这是我的 ApiDefinition.cs:

[BaseType(typeof(NSObject))]
interface Linea{
[Export ("isPresent")]
bool IsPresent();

//the delegate that will be notified of Linea events
[Export("addDelegate:")]
void AddDelegate (NSObject newDelegate);

[Export("connect")]
void Connect ();
}

如何将 objective-c 委托(delegate)转换为在 Xamarin studio 和 C# 中有用的东西?

最佳答案

现在,当我在这里发布问题时,我会自己想出解决方案...我将解决方案发布在这里不仅供我自己引用,也供其他遇到同样问题的人引用。

我找到了 this tutorial (see chapter 4.3)并认为我必须使用弱代表。像魅力一样工作!

关于objective-c - Xamarin 将 objective-c 库绑定(bind)到 C# 委托(delegate)和事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18355783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com