Xamarin iOS中没有参数的绑定方法
本文关键字:绑定 方法 参数 iOS Xamarin | 更新日期: 2023-09-27 18:25:11
我开始将iOS应用程序迁移到统一的API,但我在绑定项目中遇到了一些问题。碰巧有些委托方法不需要参数:
-(void) didFinishSendMail;
因此,当我尝试生成绑定时,会显示一个关于所需参数的错误,因此我添加了一个伪参数:
[Export ("didFinishSendMail")]
void DidFinishSendMail (bool fakeArg);
然后绑定项目编译正常,只显示一些警告。
然而,在主项目中显示了一个错误:
错误MT4117:注册器在方法"MITiOS_Binding64.MITPIntegration/MITIntegrationDelegate.DidFinishSendMail"中发现签名不匹配-选择器"DidFinishSendMail"指示该方法使用0个参数,而托管方法使用1个参数。(MT4117)
在Xamarin iOS的早期版本中,在迁移到统一的API之前,我可以添加--registrator:legacy和编译的主项目,没有任何问题,但现在这个registrator与统一的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#事件模式[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准则规定,第一个参数应该是发送消息的对象。