绑定静态全局会导致MonoTouch中出错

时间:2012-10-23 21:31:40

标签: binding xamarin.ios

我从一个功能正常的绑定项目开始,但我需要为状态标志添加一个全局int,我无法在没有错误的情况下绑定它。我从示例代码开始,无法使其工作。

我添加到我的绑定文件的代码是:

[Static]
interface CameraEffects {
    [Field ("kCameraEffectsZoomFactorKey", "CameraLibrary")]
    NSString ZoomFactorKey { get; }
}

我得到三个错误:

obj/Debug/ios/PDFExpert/CameraEffects.g.cs(34,94): error CS0117: `MonoTouch.Constants' does not contain a definition for `CameraLibraryLibrary'
obj/Debug/ios/PDFExpert/CameraEffects.g.cs(34,76): error CS1502: The best overloaded method match for `MonoTouch.ObjCRuntime.Dlfcn.dlopen(string, int)' has some invalid arguments
obj/Debug/ios/PDFExpert/CameraEffects.g.cs(34,76): error CS1503: Argument `#1' cannot convert `object' expression to type `string'

如果我离开库,它会尝试将其分配给另一个未知常量。这似乎真的搞砸了,因为它是文件中的海峡。

2 个答案:

答案 0 :(得分:3)

我想这应该像这样绑定

[Static]
interface CameraEffects {
    [Field ("kCameraEffectsZoomFactorKey", "__Internal")]
    NSString ZoomFactorKey { get; }
}

这是由于最终应用程序,可执行文件和libxxx.a将链接并合并在一起,因此它应该可以工作。

亚历

答案 1 :(得分:0)

允许分配和检索值的另一个选项是使用MonoTouch使用的内部编组。我从Xamarin支持人那里得到了这个,注意这是用于操作int,但如果你得到正确的编组代码,它应该是你可以使用的模式。

public unsafe static partial class RDPDFGlobal
{
  static readonly IntPtr __Internal_libraryHandle = Dlfcn.dlopen (null, 0);

  public static int RDPDFFeatures {
    get {
      return Dlfcn.GetInt32 (__Internal_libraryHandle, "RDPDFKitEnabledFeatures");
    }
    set {
      var indirect = Dlfcn.dlsym (__Internal_libraryHandle, "RDPDFKitEnabledFeatures");
      if (indirect == IntPtr.Zero)
        throw new Exception ("Field 'RDPDFKitEnabledFeatures' not found.");
        Marshal.WriteInt32 (indirect, value);
    }
}