簡體   English   中英

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,並且編譯的主項目沒有問題,但是現在此registrar與統一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 ();
}

不要添加偽造的參數,這是您要解決的問題的錯誤解決方案。

不幸的是,我們不支持不帶任何參數的Objective-C委托的C#-event-pattern [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指南指出,第一個參數應該是發送消息的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM