如何将Xamarin iOS Obj-C库与编组绑定

时间:2014-05-14 21:01:18

标签: c# ios xamarin.ios xamarin marshalling

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

我准备这样的库进行测试,

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

并准备像这样的绑定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);

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

如何将iOS库与编组绑定? 这样做的信息非常少......

[罗尔夫回答后编辑]

谢谢罗尔夫,我测试了很多案例,很抱歉这么晚回复。

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;            
    }
}

不起作用。
终止时没有在runnig阶段显示任何错误。

我认为

Marshal3D* ptr = &array[0]

是一个指针,但是

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

需要指针指针 不是吗?

1 个答案:

答案 0 :(得分:6)

Xamarin.iOS绑定项目不支持自定义编组。

但我认为你不需要它:

[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,您必须添加自定义绑定(这通常在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;            
        }
    }