2016-04-20 21 views
2

我已阅读Xamarin的文档。如何将Objective-C静态库绑定到Xamarin.iOS?

这是我的测试类在Objective-C:

#import "XamarinBundleLib.h" 

@implementation XamarinBundleLib 

+(NSString *)testBinding{ 
    return @"Hello Binding"; 
} 
@end 

这很容易,只有一个方法。

这是我的C#类:

namespace ResloveName 
{ 
    [BaseType (typeof (NSObject))] 
    public partial interface IXamarinBundleLib { 
     [Static,Export ("testBinding")] 
     NSString TestBinding {get;} 
    } 
} 

然后,这是我的AppDelegate代码:

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions) 
     { 
      // Override point for customization after application launch. 
      // If not required for your application you can safely delete this method 

      string testStr = ResloveName.IXamarinBundleLib.TestBinding.ToString(); 
      System.Console.WriteLine ("testStr="+testStr); 

      return true; 
     } 

当我运行应用程序,我得到这个异常: enter image description here

的TestBinding属性为空。 我一定在某个地方出了问题,所以我该如何解决?

+0

你有没有尝试客观Sharpie? https://developer.xamarin.com/guides/cross-platform/macios/binding/objective-sharpie/ – iamIcarus

+0

尝试使用'string'绑定而不是'NSString'。如果这不起作用,出于某种原因,本地库很可能没有链接到可执行文件中(构建日志会显示这一点)。 –

+0

我尝试使用字符串而不是NSString,但这不正确。现在我想也许我的本地库有些问题,我会检查它。感谢您的建议。 –

回答

2

我写了一篇关于从ObjC代码去创建一个静态库的非常详细的博客文章,该文章适用于Xamarin.iOS绑定项目,您可以找到它here(以防万一:wink :: wink :)。

话虽这么说,如果你已经在你的手中脂肪静态库,它已添加到您的Xamarin.iOS绑定项目如下所示:

binding image

问题可能是你的libxyz.linkwith.cs缺少一些信息,如果它看起来像这样:

using ObjCRuntime; 
[assembly: LinkWith ("libFoo.a", SmartLink = true, ForceLoad = true)] 

它肯定是缺少有关你的脂肪库支持的体系结构的一些重要信息(它丢失第二个参数target),可以使用下面的命令来获取什么样的体系当前的静态库支持

xcrun -sdk iphoneos lipo -info path/to/your/libFoo.a 

,你应该得到这样的事情作为输出

Architectures in the fat file: Foo/libFoo.a are: i386 armv7 x86_64 arm64 

因此,我们知道这个静态库支持i386 armv7 x86_64 arm64,我们应该通过提供第二个参数target来提供我们的LinkWith属性支持的拱形,如下所示:

using ObjCRuntime; 
[assembly: LinkWith ("libFoo.a", LinkTarget.ArmV7 | LinkTarget.Arm64 | LinkTarget.Simulator | LinkTarget.Simulator64, SmartLink = true, ForceLoad = true)] 

还要确保LinkWith属性的第一个参数与您的静态库文件名称(在我的情况下为“libFoo.a”)匹配。


其他的事情,我会建议双重检查的是你的静态库(在我的情况libFoo.a)的Build Action正确设置为ObjcBindingNativeLibrary为显示这里:

binding image

希望这有助于!

+0

我很抱歉回答太晚。这非常有帮助,谢谢。 –

+0

如果它解决了你的问题,你可以将其标记为答案:)很高兴它帮助你 – dalexsoto