绑定 iOS 框架导致临时文件损坏

Binding iOS Framework leads to corrupted temporary files

我有一个非常庞大的 iOS 框架要绑定。 这个框架依赖于另外两个。

我创建了绑定项目,成功生成了 ApiDefinitions.cs 文件和 StructsAndEnums.cs。到目前为止一切顺利。

现在,当我尝试在调试模式下构建它时,我收到大约 30 多个关于临时文件语法问题的错误。

这些错误仅出现在 Trampolines.g.cs 和 SupportDelegates.g.cs 文件中。 两者中最糟糕的是 SupportDelegates.g.cs 文件,如下所示:

   //
// Auto-generated from generator.cs, do not edit
//
// We keep references to objects, so warning 414 is expected

#pragma warning disable 414

using System;
using System.Drawing;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using UIKit;
using GLKit;
using Metal;
using MapKit;
using Photos;
using ModelIO;
using SceneKit;
using Contacts;
using Security;
using Messages;
using AudioUnit;
using CoreVideo;
using CoreMedia;
using QuickLook;
using CoreImage;
using SpriteKit;
using Foundation;
using CoreMotion;
using ObjCRuntime;
using AddressBook;
using MediaPlayer;
using GameplayKit;
using CoreGraphics;
using CoreLocation;
using AVFoundation;
using NewsstandKit;
using FileProvider;
using CoreAnimation;
using CoreFoundation;

namespace System.Action`1[[Foundation.NSString, Xamarin.iOS, Version=0.0.0 {
    public delegate void 0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]] (NSString obj);
}

Trampolines.g.cs 文件几乎完全生成,但生成的 C# 代码中间出现了同样的问题:

static internal class SDActionArity2V1 {
            static internal readonly DActionArity2V1 Handler = Invoke;

            [MonoPInvokeCallback (typeof (DActionArity2V1))]
            static unsafe void Invoke (IntPtr block, IntPtr arg1, IntPtr arg2) {
                var descriptor = (BlockLiteral *) block;
                var del = (global::System.Action<NSString, global::System.Action<NSString>>) (descriptor->Target);
                if (del != null)
                    del ( Runtime.GetNSObject<NSString> (arg1), (System.Action`1[Foundation.NSString]) Marshal.GetDelegateForFunctionPointer (arg2, typeof (System.Action`1[Foundation.NSString])));
            }
        }

其他一些绑定项目正在生成相同类型的文件,一切似乎都运行良好,所以我实际上正在努力寻找这个问题的根本原因。

有什么想法吗?

编辑: 在这里提供更多信息是生成的接口定义 https://gist.github.com/Miiite/367e17335431e932acdc92c65b504bd4

EDIT2:这是 sharpie 生成的日志输出 https://gist.github.com/Miiite/e272622df2e853d53f1add4c8eb4eabf

我认为问题出在这里: void PresentCheckoutForSubmittingTransactionCompletionHandler(Action<OPPTransaction, NSError> completionHandler, [NullAllowed] Action<NSString, Action<NSString>> paymentBrandSelectedHandler, [NullAllowed] Action cancelHandler);

我认为是 Action 中的 Action 导致了问题,生成器可能不支持此功能,因此您可能需要找到其他绑定方式。

这个有问题的方法绝对可以绑定,如果你需要的话这里是如何绑定的(为了简洁我省略了其余的class成员):

delegate void OPPPaymentBrandSelectedHandler (string paymentBrand, [BlockCallback] OPPPaymentCompletionHandler completionHandler);
delegate void OPPPaymentCompletionHandler ([NullAllowed] string checkoutId);

[BaseType (typeof (NSObject))]
interface OPPCheckoutProvider {

    // -(void)presentCheckoutForSubmittingTransactionCompletionHandler:(void (^ _Nonnull)(OPPTransaction * _Nullable, NSError * _Nullable))completionHandler
    // paymentBrandSelectedHandler:(void (^ _Nullable)(NSString * _Nonnull, void (^ _Nonnull)(NSString * _Nullable)))paymentBrandSelectedHandler 
    // cancelHandler:(void (^ _Nullable)(void))cancelHandler;
    [Export("presentCheckoutForSubmittingTransactionCompletionHandler:paymentBrandSelectedHandler:cancelHandler:")]
    void PresentCheckoutForSubmittingTransactionCompletionHandler(Action<OPPTransaction, NSError> completionHandler, [NullAllowed] OPPPaymentBrandSelectedHandler paymentBrandSelectedHandler, [NullAllowed] Action cancelHandler);
}

诀窍是不使用嵌套的 Actions,而是需要将其分成两个委托并使用 BlockCallback 属性来指示生成器该参数应作为 ObjC 块进行封送处理,然后只需使用父委托作为 PresentCheckoutForSubmittingTransactionCompletionHandler 方法中 paymentBrandSelectedHandler 参数的类型。

希望这有助于让您完全畅通无阻。也就是说我们应该报告一个更好的错误,我会和团队讨论并看看。

干杯!