1

我正在尝试在 Xamarin 中的 Objective-C 库和 C# 之间进行绑定。我试图绑定的类头是这样的:

@interface MGLPolyline : MGLMultiPoint <MGLOverlay>

+ (instancetype)polylineWithCoordinates:(CLLocationCoordinate2D *)coords
                              count:(NSUInteger)count;

我想不通的是该函数的第一个参数是什么。我试过这样绑定:

Static][Export("polylineWithCoordinates:count:")]
    [Internal]
    MGLPolyLine PolyLineWithCoordinates(IntPtr coord, int count);



public partial class MGLPolyLine
{
    public static unsafe MGLPolyLine PolyLineWithCoordinates(CLLocationCoordinate2D[] coords) 
    {
        unsafe
        {
            GCHandle handle = GCHandle.Alloc(coords);
            IntPtr pointer = (IntPtr)handle;

            MGLPolyLine line = MGLPolyLine.PolyLineWithCoordinates(pointer,2);

            handle.Free();
            return line;
        }

    }
}

上面的代码总是从 MGLPolyLine.PolyLineWithCoordinates(pointer, 2) 调用返回 null,这让我相信我没有正确传递数组。执行此绑定的正确方法是什么?

谢谢

编辑

我使用了 Objective-Sharpie 来查看它会为我创建什么绑定,这就是我得到的:

// +(instancetype)polygonWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count;
[Static]
[Export ("polygonWithCoordinates:count:")]
unsafe MGLPolygon PolygonWithCoordinates (CLLocationCoordinate2D* coords, nuint count);

现在的问题是我收到错误“btouch: Unknown kind CoreLocationCoordinate2D* coords in method 'MGLPolygon.PolygonWithCoordinates' (B1002)

4

1 回答 1

1

原来我错误地使用了 GCHandle。工作解决方案是:

   //ApiDefinition.cs
    [Static][Export ("polylineWithCoordinates:count:")][Internal]
 MGLPolyline PolylineWithCoordinates (IntPtr coords, nuint count);

// Extra.cs
public partial class MGLPolyline
{
    public static unsafe MGLPolyline PolylineWithCoordinates(CLLocationCoordinate2D[] coords)
    {
        MGLPolyline line = null;

        fixed(void* arrPtr = coords)
        {

            IntPtr ptr = new IntPtr(arrPtr);
            line = MGLPolyline.PolylineWithCoordinates(ptr, 2);   
        }

        return line;
    }
}
于 2015-08-31T18:14:03.823 回答