Xamarin iOS中没有参数的绑定方法

时间:2015-12-01 17:23:38

标签: c# ios objective-c binding xamarin

我开始将我的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不兼容。

所以问题是,使用统一API绑定不带参数的委托方法的方法是什么?

添加了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 ();
}

1 个答案:

答案 0 :(得分:1)

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

不幸的是,我们不支持C#-event-pattern,而Objective-C代表不接受任何参数[1]。

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

在API定义中定义类如下:

[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指南说第一个参数应该是发送消息的对象。