简体   繁体   English

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

[英]Binding method with no parameters in Xamarin iOS

I started migrating my iOS app to unified API, however I am having some issues with the binding project. 我开始将iOS应用程序迁移到统一API,但是绑定项目遇到一些问题。 It happens that some of the delegate methods don't require parameters: 碰巧某些委托方法不需要参数:

-(void) didFinishSendMail;

So, when I try to generate the binding, an error about a required parameter is shown so I add a fake param: 因此,当我尝试生成绑定时,会显示有关必需参数的错误,因此我添加了一个伪参数:

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

Then the binding project compiles ok and only some warnings are displayed. 然后,绑定项目编译成功,并且仅显示一些警告。

However in the main project an error is displayed: 但是,在主项目中显示错误:

Error MT4117: The registrar found a signature mismatch in the method 'MITiOS_Binding64.MITIntegration/_MITIntegrationDelegate.DidFinishSendMail' - the selector 'didFinishSendMail' indicates the method takes 0 parameters, while the managed method has 1 parameters. 错误MT4117:注册服务商在方法'MITiOS_Binding64.MITIntegration / _MITIntegrationDelegate.DidFinishSendMail'中发现签名不匹配-选择器'didFinishSendMail'表明该方法使用0个参数,而托管方法具有1个参数。 (MT4117) (MT4117)

In previous version of Xamarin iOS and before migrating to Unified API I was able to add --registrar:legacy and the main project compiled with no problems, but now this registrar is not compatible with unified API. 在以前版本的Xamarin iOS中,在迁移到Unified API之前,我能够添加--registrar:legacy,并且编译的主项目没有问题,但是现在此registrar与统一API不兼容。

So the question is, what is the way to bind a delegate method that takes no parameters using unified API? 所以问题是,使用统一的API绑定不带参数的委托方法的方式是什么?

Api Definition added: 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 ();
}

Don't add the fake parameter, that's the wrong solution for the problem you're trying to solve. 不要添加伪造的参数,这是您要解决的问题的错误解决方案。

Unfortunately you we don't support the C#-event-pattern with Objective-C delegates that don't take any parameters [1]. 不幸的是,我们不支持不带任何参数的Objective-C委托的C#-event-pattern [1]。

The solution is to use the Objective-C delegate pattern instead. 解决方案是改用Objective-C委托模式。

In the API definition define the class like this: 在API定义中,定义此类,如下所示:

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

    // ...
}

and then use it like this: 然后像这样使用它:

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] The Objective-C guidelines say that the first argument should be the object that sends the message. [1] Objective-C指南指出,第一个参数应该是发送消息的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM