Xamarin 中无参数的绑定方法 iOS

Binding method with no parameters in Xamarin iOS

我开始将我的 iOS 应用程序迁移到统一的 API,但是我在绑定项目方面遇到了一些问题。碰巧有些委托方法不需要参数:

-(void) didFinishSendMail;

所以,当我尝试生成绑定时,显示了一个关于必需参数的错误,所以我添加了一个假参数:

[Export ("didFinishSendMail")]
void DidFinishSendMail (bool fakeArg);

然后绑定项目编译正常,只显示一些警告。

但是在主项目中显示错误:

错误 MT4117:注册商发现方法 'MITiOS_Binding64.MITIntegration/_MITIntegrationDelegate.DidFinishSendMail' 中的签名不匹配 - 选择器 'didFinishSendMail' 指示该方法采用 0 个参数,而托管方法具有 1 个参数。 (MT4117)

在以前版本的 Xamarin iOS 和迁移到 Unified 之前 API 我能够添加 --registrar:legacy 并且编译的主项目没有问题,但现在这个注册商不是兼容统一 API.

那么问题来了,如何绑定一个不带参数的delegate方法,使用unifiedAPI?

Api 添加定义:

[Model,Protocol,Preserve, BaseType (typeof (NSObject), Name = "MITIntegrationDelegate")]
public partial interface MITIntegrationDelegate {


    [Export ("didFinishCommandWithError:"), EventArgs("MitError")][Abstract]
    void DidFinishCommandWithError (MITError error);


    [Export ("didFinishTransaction:"), EventArgs("MitTransaction")][Abstract]
    void DidFinishTransaction (MITTransaction transaction);


    [Export ("didFinishCardReader:"), EventArgs("MitCardInformation")][Abstract]
    void DidFinishCardReader (MITCardInformation cardInformation);


    [Export ("didFinishTransactionWithMerchant:"), EventArgs("Merchants")][Abstract]
    void DidFinishTransactionWithMerchant (NSMutableArray merchants);


    [Export ("didFinishTransactionWithMerchant:merchantsMCI:merchantsMSI:error:"), EventArgs("MerchantsPDC")][Abstract]
    void DidFinishTransactionWithMerchantPDC (NSMutableArray merchantsPDC, NSMutableArray merchantsMCI, NSMutableArray merchantsMSI, MITError error);

    [Export ("didFinishTransactionWithLocalDccOption:andForeignDccOption:"), EventArgs("LocalDccOption")][Abstract]
    void DidFinishTransactionWithLocalDccOption(MITDccOption localOption, MITDccOption foreignOption);

    [Export ("onReturnEmvApplications:"), EventArgs("EmvApplications")][Abstract]
    void OnReturnEmvApplications (NSMutableArray applicationArray);

    [Export ("didFinishSendMailWithError:"), EventArgs("MitError")]
    void DidFinishSendMailWithError (MITError error);

    [Export ("didFinishSendElectronicBillWithMessage:error:"), EventArgs("SendElectronicBill")][Abstract]
    void DidFinishSendElectronicBillWithMessage (string message, MITError error);

    [Export ("didFinishSendMail"), EventArgs("Fake")]
    void DidFinishSendMail (bool fakeArg);

    [Export ("didFinishUploadSignature:"), EventArgs("Fake")]
    void DidFinishUploadSignature (bool fakeArg);

    [Export ("didFinishUploadSignatureWithError:"), EventArgs("MitError")]
    void DidFinishUploadSignatureWithError (MITError error);

    [Export ("onNoDeviceDetected:"), EventArgs("Fake")]
    void OnNoDeviceDetected (bool fakeArg);

    [Export ("onWaitingForCard:"), EventArgs("Fake")]
    void OnWaitingForCard (bool fakeArg);

    [Export ("onDeviceUnplugged:"), EventArgs("Fake")]
    void OnDeviceUnplugged (bool fakeArg);

    [Export ("onRequestTextInfo:"), EventArgs("RequestTextInfo")]
    void OnRequestTextInfo (string textInfo);


    [Export ("onReturnWalkerUid:newUid:"), EventArgs("ReturnWalkerUid")]
    void OnReturnWalkerUid (string uid, string newUid);


    [Export ("onReturnWalkerBatteryLevel:"), EventArgs("ReturnBatteryLevel")]
    void OnReturnWalkerBatteryLevel (string batteryLevel);


    [Export ("onReturnTransactions:error:"), EventArgs("ReturnTransactions")]
    void OnReturnTransactions (NSArray transactions, MITError error);
}

[BaseType (typeof (NSObject),Name = "MITIntegration", Delegates=new string [] { "WeakDelegate" }, 
    Events=new Type [] { typeof (MITIntegrationDelegate)})]
public partial interface MITIntegration {

    [Export ("delegate", ArgumentSemantic.Assign)]
    NSObject WeakDelegate { get; set; }

    [Wrap ("WeakDelegate"), NullAllowed]
    MITIntegrationDelegate Delegate { get; set; }

    [Export ("cardInformation", ArgumentSemantic.Retain)]
    MITCardInformation CardInformation { get; set; }

    [Export ("url")]
    string Url { set; }

    [Export ("deviceConnect")]
    bool DeviceConnect { get; }

    [Export ("deviceDissconnect")]
    void DeviceDissconnect ();

    [Export ("getConnectionID")]
    string GetConnectionID { get; }

    [Export ("getName")]
    string GetName { get; }

    [Export ("getManufacturer")]
    string GetManufacturer { get; }

    [Export ("getModelNumber")]
    string GetModelNumber { get; }

    [Export ("getSerialNumber")]
    string GetSerialNumber { get; }

    [Export ("getFirmwareRevision")]
    string GetFirmwareRevision { get; }

    [Export ("getHardwareRevision")]
    string GetHardwareRevision { get; }

    [Export ("getTransactionsWithUser:password:")]
    void getTransactionsWithUser (string user, string password);

    [Export ("sndEmvDirectSellWithAmount:company:branch:user:password:usrTrx:merchant:reference:operationType:country:currency:amexCvv:")]
    void SndEmvDirectSellWithAmount (string amount, string company, string branch, string user, string password, string usrTrx, string merchant, string reference, string operationType, string country, string currency, string amexCvv);

    [Export ("sndMsrDirectSellWithAmount:company:branch:user:password:usrTrx:merchant:reference:operationType:country:currency:amexCvv:")]
    void SndMsrDirectSellWithAmount (string amount, string company, string branch, string user, string password, string usrTrx, string merchant, string reference, string operationType, string country, string currency, string amexCvv);


    [Export ("sndReprintVoucherWithUser:password:company:branch:folio:")]
    void SndReprintVoucherWithUser (string user, string password, string company, string branch, string folio);

    [Export ("sndCancelTransacctionWithUser:password:company:branch:folio:auth:amount:")]
    void SndCancelTransacctionWithUser (string user, string password, string company, string branch, string folio, string auth, string amount);


    [Export ("sndPay:")]
    void SndPay (string merchant);

    [Export ("sndEmailWithAddress:copyAddress:folio:User:password:company:branch:")]
    void SndEmailWithAddress (string address, string copyAddress, string folio, string user, string password, string company, string branch);

    [ExportAttribute("sndTaeServiceEmailWithAddress:copyAddress:folio:user:password:company:branch:idTaeCategory:idTaeProvider:idTaeProduct:")]
    void SndTaeServiceEmailWithAddress (string address, string copyAddress, string folio, string user, string password, string company, string branch, string idTaeCategory, string idTaeProvider, string idTaeProduct);

    [Export ("sndSmsWithCellPhoneNumber:carrier:folio:company:branch:User:password:")]
    void sndSmsWithCellPhoneNumber (string cellPhoneNumber, string carrier, string folio, string company, string branch, string user, string password);

    [Export ("uploadSignatureWithImage:folio:")]
    void UploadSignatureWithImage (UIView view, string folio);

    [Export ("device")]
    nint Device { set; }

    [Export ("monthsToPay")]
    string MonthsToPay { set; }

    [Export ("sndElectronicBillWithUser:company:branch:amount:ticket:folio:concept:ccNumber:billType:emailAddress:")]
    void SndElectronicBillWithUser(string user, string company,string branch,string amount,string ticket,string folio,string concept,string ccNumber,string billType,string emailAddress);

    [Export ("cancelWalkerReader")]
    void CancelWalkerReader ();

    [Export ("cancelWalkerTransaction")]
    void CancelWalkerTransaction ();

    [Export ("getWalkerUid")]
    void GetWalkerUid ();

    [Export ("getWalkerBatteryLevel")]
    void GetWalkerBatteryLevel ();
}

不要添加虚假参数,这是您要解决的问题的错误解决方案。

很遗憾,我们不支持带有 Objective-C 不带任何参数的委托的 C# 事件模式 [1]。

解决方案是改用 Objective-C 委托模式。

在 API 定义中定义 class 如下:

[BaseType (typeof (NSObject),Name = "MITIntegration"]
public partial interface MITIntegration {
    [Export ("delegate", ArgumentSemantic.Assign), NullAllowed]
    MITIntegrationDelegate Delegate { get; set; }

    // ...
}

然后像这样使用它:

class MyClass {
    MITIntegration integration;
    MyIntegrationDelegate integration_delegate;
    void Initialize ()
    {
        integration = new MITIntegration ();
        integration.Delegate = integration_delegate; 
    }
}

class MyIntegrationDelegate : MITIntegrationDelegate {
    public override void DidFinishSendMail ()
    {
        // handle this somehow
    }
}

[1] Objective-C 准则规定第一个参数应该是发送消息的对象。