1

我正在为 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 :)

4

1 回答 1

1

一种解决方案是绑定您突出显示的第二种方法

[Static]
[Export ("initWithApplifierID:withWindow:supportedOrientationsArray:")]
Applifier InitWithApplifierID (string applifierID, UIWindow withWindow, NSMutableArray orientationsArray);

现在你可以像这样使用那个ctor

NSNumber faceDown = NSNumber.FromInt32( (int) UIDeviceOrientation.FaceDown);
NSNumber faceUp = NSNumber.FromInt32( (int) UIDeviceOrientation.FaceUp);
NSNumber landscapeLeft = NSNumber.FromInt32( (int) UIDeviceOrientation.LandscapeLeft);
NSNumber landscapeRight = NSNumber.FromInt32( (int) UIDeviceOrientation.LandscapeRight);
NSNumber portrait = NSNumber.FromInt32( (int) UIDeviceOrientation.Portrait);
NSNumber portraitUpsideDown = NSNumber.FromInt32( (int) UIDeviceOrientation.PortraitUpsideDown);
NSNumber unknown = NSNumber.FromInt32( (int) UIDeviceOrientation.Unknown);

NSMutableArray orientationsArray = new NSMutableArray(7); // Set here the number of orientations wanted

orientationsArray.AddObjects( new NSObject[7] { faceDown, faceUp, landscapeLeft, landscapeRight, portrait, portraitUpsideDown, unknown } ); // add here the orientations wanted


var applifier = Applifier.InitWithApplifierID(applifierID, withWindow, orientationsArray);

有点难看,但应该可以,希望这会有所帮助

亚历克斯

于 2012-10-25T03:15:01.323 回答