如何绑定一个Objective-C的静态库Xamarin.iOS? [英] How to bind an Objective-C static library to Xamarin.iOS?
问题描述
我已经阅读从Xamarin的文档。
I have read the documentation from Xamarin.
这是我的测试类在Objective - C:
And this is my test class in Objective-C:
#import "XamarinBundleLib.h"
@implementation XamarinBundleLib
+(NSString *)testBinding{
return @"Hello Binding";
}
@end
这很简单,只有一个方法。
It's very easy, just one method.
这是我的C#类:
namespace ResloveName
{
[BaseType (typeof (NSObject))]
public partial interface IXamarinBundleLib {
[Static,Export ("testBinding")]
NSString TestBinding {get;}
}
}
然后,这是我的AppDelegate code:
Then this is my AppDelegate code:
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;
}
When I run the application, I get this exception:
该TestBinding的属性为null。
我一定是错的地方,所以我怎么能解决这个问题?
The TestBinding attributes is null. I must be wrong somewhere, so how can I fix it?
推荐答案
我写了一篇关于从去年开始,关于Xamarin.iOS结合项目工程,你可以找到它ObjC code创建静态库的一个非常详细的博客文章这里(以防万一:眨眼::眨眼:)。
I wrote a very detailed blog post about creating a static library from ObjC code last year that works on Xamarin.iOS binding projects and you can find it here (just in case :wink::wink:).
话虽这么说,如果你已经在你的手中脂肪静态库,它已添加到您的Xamarin.iOS绑定项目如下所示:
That being said if you already have a fat static library in your hands and it is already added into your Xamarin.iOS Binding Project as shown here:
问题可能是你的 libxyz.linkwith.cs
缺少一些信息,如果它看起来是这样的:
The issue could be that your libxyz.linkwith.cs
is missing some information, if it looks like this:
using ObjCRuntime;
[assembly: LinkWith ("libFoo.a", SmartLink = true, ForceLoad = true)]
这绝对缺少有关你的脂肪库(它缺少第二个参数目标
),可以使用下面的命令来检索的支持的体系结构的一些重要信息体系结构的当前静态库支持
it is definitely missing some important information about the architectures supported by your fat library (it is missing the second argument target
), you can use the following command to retrieve what architectures your current static library supports
xcrun -sdk iphoneos lipo -info path/to/your/libFoo.a
和你应该得到这样的事情作为输出
and you should get something like this as output
Architectures in the fat file: Foo/libFoo.a are: i386 armv7 x86_64 arm64
因此,我们知道这个静态库的支持 I386的ARMv7 x86_64的arm64
,我们应该为我们的 LinkWith
属性支持archs通过提供第二个参数目标
如下:
So we know this static library supports i386 armv7 x86_64 arm64
and we should provide our LinkWith
attribute the supported archs by providing the second argument target
as follows:
using ObjCRuntime;
[assembly: LinkWith ("libFoo.a", LinkTarget.ArmV7 | LinkTarget.Arm64 | LinkTarget.Simulator | LinkTarget.Simulator64, SmartLink = true, ForceLoad = true)]
另外,还要确保在 LinkWith
属性的第一个参数(在我的情况libfoo.a中)匹配您的静态库文件名。
Also make sure that the first parameter of the LinkWith
attribute matches your static library file name ("libFoo.a" in my case).
其他的事情我会建议双重检查的是,生成操作
您的静态库( libfoo.a中
在我的情况)正确设置为 ObjcBindingNativeLibrary
为展示位置:
The other thing I would suggest double checking is that the Build Action
of your static library (libFoo.a
in my case) is correctly set to ObjcBindingNativeLibrary
as show here:
希望这有助于!
这篇关于如何绑定一个Objective-C的静态库Xamarin.iOS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!