gpt4 book ai didi

c# - 如何在 Xamarin.iOS 中包装 Objective-C 库?

转载 作者:行者123 更新时间:2023-11-30 20:36:36 25 4
gpt4 key购买 nike

头文件包括以下内容:

#include <Foundation/Foundation.h>

FOUNDATION_EXPORT NSString* GoGopherExecute(NSString* action, NSString* data);

我看过How do I correctly wrap native c library in Xamarin.iOS ,这似乎最接近我正在尝试做的事情。我还在 Xamarin 网站上看到了资源 https://developer.xamarin.com/guides/cross-platform/macios/binding/objective-c-libraries/ .我的 ApiDefinition.cs 中有以下内容

[DllImport("Gopher.a", 
EntryPoint="GoGopherExecute",
CallingConvention = CallingConvention.Cdecl)]
public static NSString GoExecute(NSString action, NSString data);

我得到的错误是“意外的符号‘NSString’,需要‘class’、‘delegate’、‘enum’、‘interface’、‘partial’或‘struct’”。我应该在 ApiDefinition.cs 中包含什么?

最佳答案

当您接近时,您确实需要一些额外的步骤才能使其完全正常工作。但首先你需要掌握 Xamarin.iOS 绑定(bind)项目的一些概念。

这是 Xamarin.iOS 绑定(bind)项目结构:

enter image description here

大多数时候您只需要项目模板附带的两个文件(我手动添加了 Extras.cs):

引自 Xamarin Documentation

ApiDefinition.cs: will only contain namespaces and interface definitions (with any members that an interface can contain) and should not contain classes, enumerations, delegates or structs. The API definition file is merely the contract that will be used to generate the API.

StructsAndEnums.cs: any enums, types, structs required by the API definition file.

因此,在 ApiDefinition 中需要注意的一件事是它只能包含 ObjC 定义,因此您上面的代码必须添加到一个额外的 c# 代码文件中,因为它是一个 c 函数,只需将一个新类添加到您的绑定(bind)项目中我倾向于将此文件命名为 Extras.cs(您可以随意命名它)。

// Extras.cs contents
using System;
using System.Runtime.InteropServices;
using Foundation;
using ObjCRuntime;

namespace FooBinding {
public static class GoGopherCFuncs {
// FOUNDATION_EXPORT NSString* GoGopherExecute(NSString* action, NSString* data);
[DllImport ("__Internal", EntryPoint= "GoGopherExecute")]
static extern IntPtr _GoExecute (IntPtr action, IntPtr data);

public static string GoExecute (string action, string data)
{
// Avoid creating any unused managed refs of NSString and get a handle directly
// from our managed NET strings
IntPtr actionPtr = NSString.CreateNative (action);
IntPtr dataPtr = NSString.CreateNative (data);

IntPtr ptr = _GoExecute (actionPtr, dataPtr);

// Manually release our native NSString handles
NSString.ReleaseNative (actionPtr);
NSString.ReleaseNative (dataPtr);

// Return a NET string from our unmanaged handle returned by _GoExecute.
return (string) Runtime.GetNSObject<NSString> (ptr);
}
}
}

所以上面的代码应该足以解决您的问题并让您开始绑定(bind)任何额外的 c API。

DllImport属性中还有一个你需要修改的东西,你需要将dllName更改为__Internal,这是因为它Gopher.a 很可能是一个静态库(您可以检查 this other answer 以确认它是否是一个静态库)所以它将“合并”到您的主要可执行文件中,因此您只需要将 Gopher.a 添加到您的绑定(bind)项目中,如 Xamarin Documentation 中所述.

引自 Xamarin Documentation

To complete this binding, you should add the native library to the project. You can do this by adding the native library to your project, either by dragging and dropping the native library from Finder onto the project in the solution explorer ...

我真的鼓励您阅读 Xamarin Documentation :)

希望这对您有所帮助!

关于c# - 如何在 Xamarin.iOS 中包装 Objective-C 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36755982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com