带委托的绑定方法 [英] Bind Method With Delegate
问题描述
我正在创建一个绑定库来绑定本机Objective-C框架.
I'm creating a binding library to bind native Objective-C framework.
我有以下代表,我需要将其声明添加到ApiDefinition文件中,然后需要使用Xamarin.iOS应用程序来实现它:
I have the following delegate which I need to add it's declaration to ApiDefinition file and then I need to implement it using my Xamarin.iOS app:
- (void)Initialize:(id <MMSDKDelegate> _Nullable)pDelegate;
MMSDKDelegate:
@protocol MMSDKDelegate <IMMDelegate>
- (void)Started;
@end
IMMDelegate:
@protocol IMMDelegate
- (void)Inserted;
- (void)Removed;
- (void)ReaderConnected;
- (void)ReaderRemoved;
@end
我需要ApiDefinition文件中的必需定义,并且我需要一个示例代码来从我的Xamarin.iOS应用程序中调用此方法.
I need the required definition in ApiDefinition file and I need a sample code to call this method from my Xamarin.iOS app.
更新
我正在处理的框架正在与读卡器设备与iPhone相连以读取ID信息,它具有在插入/移除和删除阅读器时调用的方法卡已插入/已拔出..
The Framework I'm dealing with is communicating with card reader device attached with iPhone to read ID card info, it has methods to be called on reader inserted / removed & card inserted / removed..
我已经通过@ cole-xia实现了答案,但是问题是IMMDelegate
中的方法永远不会在插入读卡器或ID时调用.当我调用ReadCardData()
时,它应该调用Started()
,这将显示Inserted()
保存的信息,但是当前结果是在调用ReadCardData()
之后调用了Started()
方法,但是Inserted()
和ReaderConnected()
是从未在任何阶段打电话.
I have implemented the answer by @cole-xia, but the issue is the methods inside IMMDelegate
are never called when I insert card reader or ID. When I call ReadCardData()
, it should call Started()
which will display information saved by Inserted()
, but the current result is that Started()
method is called after calling ReadCardData()
, but Inserted()
and ReaderConnected()
are never called in any stage.
在Framework的演示应用程序中,它可用于以下目的(并且可以正常运行):
In the demo app of Framework, it is used as the following (and works properly):
// Demo app -> ViewController.m
@interface ViewController () <MMSDKDelegate>
@end
@implementation ViewController {
MMSDK *sdkInstance;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
sdkInstance = [MMSDK SharedInstance];
[sdkInstance Initialize:self];
}
- (void)Started
{
// Update UI by: reading in progress ..
}
- (void)Inserted
{
// Update UI by: card inserted
// And read card data
[self readData:self];
}
- (void)Removed
{
// Update UI by: card removed
}
- (void)ReaderConnected
{
// Update UI by: card reader connected
}
- (void)ReaderRemoved
{
// Update UI by: card reader removed
}
- (IBAction)readData:(id)sender
{
var *data = [sdkInstance ReadCardData:YES pWithSignatureImage:YES pWithAddressData:YES];
if (data.hasError) {
// No data fetched
return;
}
if (data) {
// return card data
}
}
欢迎并提出所有建议.
总而言之,我只需要在Xamarin.iOS应用中执行演示应用的相同功能即可.
推荐答案
使用 我这边的结果:
// @protocol IMMDelegate
[BaseType (typeof (NSObject))]
[Protocol, Model]
interface IMMDelegate
{
// @required -(void)Inserted;
[Abstract]
[Export ("Inserted")]
void Inserted ();
// @required -(void)Removed;
[Abstract]
[Export ("Removed")]
void Removed ();
}
// @protocol MMSDKDelegate <IMMDelegate>
[BaseType (typeof (NSObject))]
[Protocol, Model]
interface MMSDKDelegate : IMMDelegate
{
// @required -(void)Started;
[Abstract]
[Export ("Started")]
void Started ();
}
// @interface ACR : NSObject
[BaseType (typeof(NSObject))]
interface YourClass
{
// -(void)Initialize:(id<MMSDKDelegate> _Nullable)pDelegate;
[Export ("Initialize:")]
void Initialize ([NullAllowed] MMSDKDelegate pDelegate);
}
用法:
class MyDelegate : MMSDKDelegate {
public void Started()
{
}
public override void Removed()
{
}
public override void Inserted()
{
}
}
//In ViewController
public override void ViewDidLoad()
{
base.ViewDidLoad();
YourClass yourClass = new YourClass();
yourClass.Initialize(new MyDelegate());
}
这篇关于带委托的绑定方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!