我正在为 Applifier Obj-C API 创建一个最小的 MonoTouch 绑定。它包含这个 init 方法:
+ (Applifier *)initWithApplifierID:(NSString *)applifierID withWindow:(UIWindow *)window
supportedOrientations:(UIDeviceOrientation)orientationsToSupport, ...NS_REQUIRES_NIL_TERMINATION;
按照可变参数方法的绑定文档中的说明,我想出了这个接口方法:
[Static]
[Export ("initWithApplifierId:withWindow:supportedOrientations:"), Internal]
void InitWithApplifierId (string applifierID, UIWindow withWindow,
UIDeviceOrientation supportedOrientations, IntPtr orientationsPtr);
以及我的扩展中的这个公共方法
public static Applifier InitWithApplifierId(string applifierId, UIWindow window,
params UIDeviceOrientation[] supportedOrientations)
{
if (supportedOrientations == null)
throw new ArgumentNullException ("supportedOrientations");
var pNativeArr = Marshal.AllocHGlobal(supportedOrientations.Length * IntPtr.Size);
for (int i = 1; i < supportedOrientations.Length; ++i) {
Marshal.WriteIntPtr (pNativeArr, (i - 1) * IntPtr.Size,
supportedOrientations[i].Handle);
}
// Null termination
Marshal.WriteIntPtr (pNativeArr, (supportedOrientations.Length - 1) * IntPtr.Size,
IntPtr.Zero);
Applifier.InitWithApplifierId(applifierId, window, supportedOrientations[0],
pNativeArr);
Marshal.FreeHGlobal(pNativeArr);
}
但是,UIDeviceOrientation 是一个枚举而不是一个对象,所以没有 Handle 可以写。我对 Objective-c 非常陌生,对 C# 也很陌生(我的项目实际上是通过 IKVM 与 MonoTouch 交互的;我的专长是 Java)。我尝试对supportedOrientation[i] 本身进行天真的Marshal.WriteInt32,但这在编译时也失败了。
如果它更容易,我可以绑定此方法的重载:
+ (Applifier *)initWithApplifierID:(NSString *)applifierID withWindow:(UIWindow *)window
supportedOrientationsArray:(NSMutableArray *)orientationsArray;
但是,我也不确定如何绑定 NSMutableArray :)