Objective-C 将指针数组绑定到 C#
Objective-C binding pointer array to C#
我正在尝试在 Xamarin 中建立 Objective-C 库和 C# 之间的绑定。我要绑定的 class header 是这样的:
@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;
}
}
}
上面的代码总是 returns 来自 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:未知类型的 CoreLocationCoordinate2D* 坐标 'MGLPolygon.PolygonWithCoordinates' (B1002)
原来我错误地使用了 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;
}
}
我正在尝试在 Xamarin 中建立 Objective-C 库和 C# 之间的绑定。我要绑定的 class header 是这样的:
@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;
}
}
}
上面的代码总是 returns 来自 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:未知类型的 CoreLocationCoordinate2D* 坐标 'MGLPolygon.PolygonWithCoordinates' (B1002)
原来我错误地使用了 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;
}
}