c# - 如何使用编码绑定(bind) Xamarin iOS Obj-C 库

标签 c# ios xamarin.ios xamarin marshalling

我想研究如何将 iOS 库与指向 Xamarin.iOS 的处理指针绑定(bind)。 它需要编码。

我为测试准备了这样的库,

MarshalTest.h

typedef struct
{
    float x,y,z;
} Marshal3D;


@interface MarshalTest : NSObject

-(id)initWithMarshal:(Marshal3D *)marshal;
-(id)initWithMarshals:(Marshal3D *)marshals num:(int)numCoord;

-(void)addMarshal:(Marshal3D *)marshal;
-(void)addMarshals:(Marshal3D *)marshals num:(int)numCoord;

-(int)getMarshals:(Marshal3D **)getMarshals;

-(int) storedNumber;
-(float)checkMarshalValue:(int)index xyz:(int)xyz;

@end

并像这样准备绑定(bind) C# 代码:

StructsAndEnums.cs

using System;
using System.Runtime.InteropServices;

namespace MarshalTestNet
{
    [StructLayout(LayoutKind.Sequential)]
    public struct Marshal3D
    {
        public float x;
        public float y;
        public float z;
    };
}

ApiDefinition.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;

using MonoTouch.ObjCRuntime;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace MarshalTestNet
{
    [BaseType (typeof (NSObject))]
    public partial interface MarshalTest {

        [Export ("initWithMarshal:")]
        IntPtr Constructor ([In, MarshalAs(UnmanagedType.LPStruct)]Marshal3D marshal);

        [Export ("initWithMarshals:num:")]
        IntPtr Constructor ([In, MarshalAs(UnmanagedType.LPStruct)]Marshal3D marshals, int numCoord);

        [Export ("addMarshal:")]
        void AddMarshal ([In, MarshalAs(UnmanagedType.LPStruct)]Marshal3D marshal);

        [Export ("addMarshals:num:")]
        void AddMarshals ([In, MarshalAs(UnmanagedType.LPStruct)]Marshal3D marshals, int numCoord);

        [Export ("getMarshals:")]
        int GetMarshals ([Out, MarshalAs(UnmanagedType.LPStruct)]Marshal3D getMarshals);

        [Export ("storedNumber")]//, Verify ("ObjC method massaged into getter property", "/Users/kokogiko/Desktop/MarshalTest.h", Line = 32)]
        int StoredNumber { get; }

        [Export ("checkMarshalValue:xyz:")]
        float CheckMarshalValue (int index, int xyz);
    }
}

但这行不通,只是调用

var mars3 = new Marshal3D{ 
    x = 1.0f,
    y = 2.0f,
    z = 3.0f
};
var mars = new MarshalTest (mars3);

将终止而不显示错误消息。

如何通过编码绑定(bind) iOS 库? 执行此操作的信息非常少......

[根据 Rolf 的回答进行编辑]

谢谢 Rolf,我测试了很多案例,很抱歉这么晚才回复。

1.

[Export ("initWithMarshal:")]
IntPtr Constructor (ref Marshal3D marshal);

[Export ("addMarshal:")]
void AddMarshal (ref Marshal3D marshal);

效果很好。谢谢!

2.

[Export ("initWithMarshals:num:")]
IntPtr Constructor (Marshal3D[] marshals, int numCoord);

[Export ("addMarshals:num:")]
void AddMarshals (Marshal3D[] marshals, int numCoord);

不起作用,在编译阶段显示此错误:

obj/Debug/ios/MarshalTestNet/MarshalTest.g.cs(140,31): error CS1502: The best overloaded method match for `MonoTouch.Foundation.NSArray.FromNSObjects(params MonoTouch.Foundation.NSObject[])' has some invalid arguments
    /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll (Location of the symbol related to previous error)
obj/Debug/ios/MarshalTestNet/MarshalTest.g.cs(140,46): error CS1503: Argument `#1' cannot convert `MarshalTestNet.Marshal3D[]' expression to type `MonoTouch.Foundation.NSObject[]'

所以我自己改了,

ApiDefinition.cs

[Export ("addMarshals:num:")]
void AddMarshals (IntPtr marshals, int numCoord);

Extra.cs

public unsafe void AddMarshals (Marshal3D[] marshals) {
    var count = marshals.Length;
    fixed (Marshal3D* ptr = &marshals[0])
        AddMarshals ((IntPtr)ptr, count);         
}

这很有效。

但是,这种方法不能用于

-(id)initWithMarshals:(Marshal3D *)marshals num:(int)numCoord;

因为是构造函数,所以在调用父方法之前不允许添加任何逻辑。

有什么办法可以解决这个问题吗?
使用“工厂模式”是最好的?

3.

[Export ("getMarshals:")]
[Internal]
int GetMarshals (IntPtr getMarshals);

public partial class MarshalTest : NSObject {
    public unsafe Marshal3D [] GetMarshals () {
        var count = this.StoredNumber();
        var array = new Marshal3D [count];

        fixed (Marshal3D* ptr = &array[0])
            GetMarshals ((IntPtr) ptr);

        return array;            
    }
}

无效。
在运行阶段未显示任何错误而终止。

我觉得

Marshal3D* ptr = &array[0]

是一个指针,但是

-(int)getMarshals:(Marshal3D **)getMarshals;

需要指针中的指针。
对不对?

最佳答案

Xamarin.iOS 绑定(bind)项目不支持自定义编码。

但我认为你不需要它:

[BaseType (typeof (NSObject))]
public partial interface MarshalTest {

    [Export ("initWithMarshal:")]
    IntPtr Constructor (ref Marshal3D marshal);

    [Export ("initWithMarshals:num:")]
    IntPtr Constructor (Marshal3D[] marshals, int numCoord);

    [Export ("addMarshal:")]
    void AddMarshal (ref Marshal3D marshal);

    [Export ("addMarshals:num:")]
    void AddMarshals (Marshal3D[] marshals, int numCoord);

    [Export ("getMarshals:")]
    [Internal]
    int GetMarshals (IntPtr getMarshals);

    // ...
}

然而,为了很好地支持 GetMarshals,您必须添加自定义绑定(bind)(这通常在 StructsAndEnums.cs 文件中完成):

public partial class MarshalTest : NSObject {
    public unsafe Marshal3D [] GetMarshals () {
        var count = // how many are there? there's no API to fetch this in your sample
        var array = new Marshal3D [count];

        fixed (Marshal3D* ptr = &array[0])
            GetMarshals ((IntPtr) ptr);

        return array;            
    }
}

[已更新以回答第二组问题]

  1. 对于构造函数,我认为这会起作用:

    ApiDefinition.cs

    [BaseType (typeof (NSObject))]
    public partial interface MarshalTest {
        [Export ("initWithMarshals:num:")]
        [Internal]
        IntPtr Constructor (IntPtr marshals, int numCoord);
    }
    

    Extra.cs

    public partial class MarshalTest : NSObject {
        public MarshalTest (Marshal3D[] marshals, int numCoord)
            : this (GetPointer (marshals), numCoord)
        {
        }
    
        static IntPtr GetPointer (Marshal3D[] marshals)
        {
            fixed (Marshal3D* ptr = &marshals[0])
                return (IntPtr) ptr;
        }
    

  2. 没错,它需要一个指向数组的指针。试试这个:

    ApiDefinition.cs

    [BaseType (typeof (NSObject))]
    public partial interface MarshalTest {
        [Export ("getMarshals:")]
        [Internal]
        int GetMarshals (ref IntPtr getMarshals);
    }
    

    Extra.cs

    public partial class MarshalTest : NSObject {
        public unsafe Marshal3D [] GetMarshals () {
            var ptr = IntPtr.Zero;
            var count = this.StoredNumber ();
            var array = new Marshal3D [count];
    
            GetMarshals (ref ptr);
    
            unsafe {
                Marshal3D* ptr3d = (Marshal3D *) ptr;
                for (int i = 0; i < count; i++)
                    array [i] = *ptr3d++;
            }
    
            return array;            
        }
    }
    

关于c# - 如何使用编码绑定(bind) Xamarin iOS Obj-C 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23665042/

相关文章:

ios - 在应用程序中全局关闭自动更正

c# - 单点触控饼图

c# - 如何在MessageBox中显示textBox控件?

c# - 通用显式转换失败 C#

ios - 以编程方式定义新的导航 Controller 顺序/堆栈?

ios - 设置 Root View Controller 不起作用?

c# - DTO 加上 UnitOfWork 模式是为 Web 应用程序设计 DAL 的好方法吗?

c# - 以列表列表作为数据源的 RDLC 报告

macos - 是否可以使用 TFS 在线恢复 Xamarin.iOS 版本的 Nuget 包?

ios - Visual Studio 中仅支持 ARM64 架构