【问题标题】:Exposing an Obj-C const NSString via a MonoTouch binding通过 MonoTouch 绑定公开 Obj-C const NSString
【发布时间】:2012-04-07 13:52:52
【问题描述】:

我已经为ZBar 提供了一个有效的 MonoTouch 绑定并正在运行,但是在公开 Obj-C 库定义的用作 NSDictionary 中的键的常量 NSString 时遇到了麻烦:

在 ZBarReaderController.h 中:

extern NSString* const ZBarReaderControllerResults;  

我首先尝试通过 here 记录的实际 MonoTouch 绑定:

[Static]
interface ZBarSDK
{
    [Field ("ZBarReaderControllerResults")]
    NSString BarcodeResultsKey { get; }
}

尝试构建包含此内容的项目会导致 btouch 出现以下错误:

未处理的异常:System.ArgumentOutOfRangeException:参数超出范围。
参数名称:startIndex
在 System.String.Substring (Int32 startIndex) [0x00000] in :0
在 Generator.Generate(System.Type 类型)[0x00000] in :0
在 Generator.Go () [0x00000] in :0
在 BindingTouch.Main (System.String[] args) [0x00000] in :0
[错误] 致命的未处理异常:System.ArgumentOutOfRangeException:参数超出范围。
参数名称:startIndex
在 System.String.Substring (Int32 startIndex) [0x00000] in :0
在 Generator.Generate(System.Type 类型)[0x00000] in :0
在 Generator.Go () [0x00000] in :0
在 BindingTouch.Main (System.String[] args) [0x00000] in :0

接下来我尝试按照其他SO answer 中的建议手动调用代码。

public static NSString BarcodeResultsKey
{
    get
    {
        var libHandle = Dlfcn.dlopen("libzbar.a",0);
        // I also tried this with "__Internal", rather than "libzbar.a"
        return Dlfcn.GetStringConstant(libHandle, "ZBarReaderControllerResults");
    }
}

它可以正常构建和执行,但只返回一个空字符串(如 Dlfcn.GetStringConstant 文档,如果链接失败,它将执行此操作)。

那么,还有其他人从第 3 方 Obj-C 库中访问 c​​onst 字符串吗?

【问题讨论】:

  • 我假设您已经在您的一个实现文件中定义 ZBarReaderControllerResults,对吧?你已经链接到那个实现了吗?另外,我不希望 Dlfcn 使用静态库(.a 文件)。它适用于动态库(.dylib 文件),您需要确保它正在找到该库。请参阅dlopen(1) man page
  • 不是我个人,不 - 我假设它是在 zbar SDK 库(libzbar.a 库)中实现的,因为该声明在 SDK 提供的头文件中。是的,它肯定与该实现正确链接——我已经让库的所有重要部分都在工作(显示 ReaderViewController、捕获条形码等)。至于 dlfcn 和 dlopen,我以前从未使用过 dlopen 或做过很多 Unix 风格的本地编程,所以我不确定 - 感谢您为我清理,我将停止调查该路径。

标签: c# objective-c xamarin.ios


【解决方案1】:

生成器btouch[Field] 绑定有一个限制(5.2.11 之前),要求命名空间以MonoTouch. 开头。

issue 的快速解决方法是将命名空间从ZBar 重命名为MonoTouch.ZBar,绑定定义将正确构建。

由于 iOS 应用程序必须与应用程序中包含的库的静态库 (.a) 链接,因此还需要在绑定中提供库名称 "__Internal",如 documentation 中所述。

[Static]
interface ZBarSDK {
    [Field ("ZBarReaderControllerResults", "__Internal")]
    NSString BarcodeResultsKey { get; }
}

还有一个编译问题(在生成的代码上),需要对库进行一些手动调整(即,您可以使用 null 而不是库名称,因为它已链接到主应用程序中)。这也在 MonoTouch 5.2.11 版本中得到修复。

使用解决方法(或 MonoTouch 5.2.11)和 __Internal 更改,您应该能够在绑定中使用 [Field]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    • 2011-03-12
    • 2011-08-24
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    相关资源
    最近更新 更多