通过 MonoTouch 绑定公开 Obj-C const NSString [英] Exposing an Obj-C const NSString via a MonoTouch binding

查看:26
本文介绍了通过 MonoTouch 绑定公开 Obj-C const NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于 ZBar 的有效 MonoTouch 绑定并正在运行,但是我在暴露 Obj-C 库定义用作 NSDictionary 中的键的常量 NSString 时遇到了麻烦:

I've got a working MonoTouch binding for ZBar up and running, but am having troubles exposing a constant NSString that the Obj-C library defines for use as a Key in an NSDictionary:

在 ZBarReaderController.h 中:

inside ZBarReaderController.h:

extern NSString* const ZBarReaderControllerResults;  

我首先尝试通过实际的 MonoTouch 绑定作为记录此处:

I first tried via the actual MonoTouch binding as documented here:

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

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

Attempting to build the project containing this gave these errors from btouch:

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

Unhandled Exception: System.ArgumentOutOfRangeException: Argument is out of range.
Parameter name: startIndex
at System.String.Substring (Int32 startIndex) [0x00000] in :0
at Generator.Generate (System.Type type) [0x00000] in :0
at Generator.Go () [0x00000] in :0
at BindingTouch.Main (System.String[] args) [0x00000] in :0
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Argument is out of range.
Parameter name: startIndex
at System.String.Substring (Int32 startIndex) [0x00000] in :0
at Generator.Generate (System.Type type) [0x00000] in :0
at Generator.Go () [0x00000] in :0
at BindingTouch.Main (System.String[] args) [0x00000] in :0

我接下来尝试按照其他 SO 答案.

I next attempted to manually call into the code as suggested in this other 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 记录了链接失败时它将执行的操作.

It builds and executes fine, but just returns an empty string (as the Dlfcn.GetStringConstant documents it will do if it fails to link).

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

So, anyone else accessed const strings from a 3rd party Obj-C library?

推荐答案

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

The generator, btouch, had a limitation (before 5.2.11) for [Field] bindings that requires the namespace to start with MonoTouch..

问题的快速解决方法是从 ZBarMonoTouch.ZBar 并且绑定定义将正确构建.

A quick workaround for this issue is to rename the namespaces from ZBar to MonoTouch.ZBar and the binding definitions will build correctly.

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

Since iOS applications must link with static libraries (.a) for libraries that are included with an application it's also required to supply a library name "__Internal" in the bindings as described in the documentation.

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

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

There was also a compilation issue (on the generated code) which required some manual adjustment for the library (i.e. you can use null instead of the library name since it's linked inside the main app). This is also fixed in the MonoTouch 5.2.11 release.

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

With the workarounds (or MonoTouch 5.2.11) and the __Internal change you should be able to use [Field] inside your bindings.

这篇关于通过 MonoTouch 绑定公开 Obj-C const NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆