不能使用NSArray
除非Objective-C的代码使用NSArray
,即发电机可以让我们绘制一些ObjC结构,以.NET类型(例如NSString
到string
),但它不会让你重新定义ObjC类型。
-(BOOL)playSound:(int)volume beepData:(int *)data length:(int)length error:(NSError **)error;
应该是这样的:
[Export ("playSound:beepData:length:error:")]
bool PlaySound (int volume, IntPtr data, int length, out NSError error);
你需要列你data
到IntPtr
。
IntPtr data = Marshal.AllocHGlobal (length);
Marshal.WriteInt32 (data1, 0);
然后释放它。
Marshal.FreeHGlobal (data);
这是最好的使用它调用你的内部结合公共helper方法来完成。您可以通过在其定义中添加[Internal]
属性来制作PlaySound
方法internal
。因此,它变成了:
[Export ("playSound:beepData:length:error:")][Internal]
bool PlaySound (int volume, IntPtr data, int length, out NSError error);
,你包括你的绑定(如API.cs)下面的代码:
bool PlaySound (int volume, int[] data)
{
// I assume length is byte-based (check the docs)
int length = data.Length * 4;
IntPtr p = Marshal.AllocHGlobal (length);
int j = 0;
for (int i=0; i < length; i+=4)
Marshal.WriteInt32 (p [j++], i);
NSError error;
bool result = PlaySound (volume, p, length, out error);
// free memory before throwing the exception (if any)
Marshal.FreeHGlobal (data);
if (error != null)
throw new Exception (error.LocalizedDescription);
return result;
}
注:完全未经试验的:-)我没有硬件,SDK或文档。 YMMV,但应该是接近。
你是个绅士和学者。明天早上我会试试这个。谢谢! – jeffrapp
这是一个不行。 Mono balks在Marshal.WriteInt32(p [j ++],i)中,所以我将它改为'Marshal.WriteInt32(p,i,data [j ++])',仍然没有任何结果。也试图用GCHandle.Alloc获取IntPtr,没有任何结果。我尝试了xcode,'int sound [] = {2730,150,0,30,2730,2730}; int size = sizeof(sound); [linea playSound:100 beepData:sound length:size error:nil];',这是有效的。当调试器击中实际的PlaySound行时,应用程序输出显示' - [Linea playSound:beepData:length:error]:无法识别的选择器发送到实例0xa2e6b40',但没有发生真正的错误。 – jeffrapp
我不知道你的原始数据格式是什么。看看其他'Marshal'方法(如'Copy')来复制你的数据。你也可以尝试将它绑定为一个'(int *)',并在你的帮助器方法中使用'unsafe fixed'。但没有硬件和上下文,我们所能做的只是猜测;-) – poupou