Xamarin绑定Objective-C库

3

我希望能在Xamarin中绑定一个Objective-C库(用于使用一根电缆)。我是Xamarin平台的新手,有人能帮我将下面的.h文件转换为Xamarin绑定项目中的“ApiDefinition.cs”吗?

#import <UIKit/UIKit.h>

#ifndef CABLE_

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
@protocol CableManagerDelegate;

/*

    This protocol), describes the main interface to the Cable Socket Manager layer.

    To use, call factory method below [CableManager sharedInstance]

 */
@protocol CableManagerProtocol <NSObject>
// set delegate for cable connect callbacks
-(void)setDelegate:(id < CableManagerDelegate >) delegate;

-(BOOL)isCableConnected;

-(NSString *)getAccessoryFirmwareVersion;

@end

@protocol CableManagerDelegate <NSObject>

//Cable was connected
- (void) cableConnected:(NSString *)protocol;

// Cable was disconnected and/or application moved to background
- (void) cableDisconnected;
@end


@interface CableManager : NSObject

+ (id < CableManagerProtocol >)sharedInstance;

@end

你尝试过这个吗? 链接 - SYNT4X
在相应的Xcode项目中,通过[CableManager sharedInstance] setDelegate =self];调用setdelegate函数。在ApiDefinition.cs中,我已经编写了setDelegate函数:Interface CableManager { [Static] [Export (“SharedInstance”)] CableManagerProtocol SharedInstance {get ;} }并且我已经使用它创建了dll。但是我不知道如何使用SharedInstance调用委托函数。请有人帮助我解决这个问题。 - geethu
2个回答

2
无法为您编写,但下面的示例是一般模式,您可以在下面链接的指南中了解更多信息。您面临的主要挑战之一将是确保代理上的回调被触发。要映射此内容,请查看“绑定协议”部分。

http://developer.xamarin.com/guides/ios/advanced_topics/binding_objective-c/binding_objc_libs/

ApiDefinition.cs

using MonoTouch.ObjCRuntime;
using MonoTouch.Foundation;
using System;

namespace MyNamespace
{
  [BaseType (typeof (NSObject))]
  interface MyObjCWrapper
  {
    [Export("initWithArg1:arg2:arg3:")]
    void Constructor(string first, string second, string third);

    [Export("mySelectorTaking1arg:")] // note colon, takes 1 arg
    void DoSomethingWith1Arg(string filePath);

    [Export("getSomething")] // note no colon, takes 0 args
    int GetSomething();
}

为了完成这个绑定,您应该将本地库添加到项目中。您可以通过将本地库从Finder拖放到解决方案资源管理器中的项目中,或右键单击项目并选择添加>添加文件来将本地库添加到项目中。按照惯例,本地库以“lib”开头并以“.a”扩展名结尾。当您这样做时,Xamarin Studio将添加两个文件:.a文件和一个自动填充的C#文件,其中包含有关本地库包含内容的信息:
您最终将得到一个类似于这样的文件(libLibraryName.linkwith.cs):
using System;
using MonoTouch.ObjCRuntime;

[assembly: LinkWith ("libLibraryName.a", SmartLink = true, ForceLoad = true)]

1
使用Objective-Sharpie。它将为您完成初步的大部分工作,您只需要填补空白即可...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接