编程语言
首页 > 编程语言> > C#-Xamarin绑定目标C库

C#-Xamarin绑定目标C库

作者:互联网

我想在xamarin中绑定一个目标c库(用于使用电缆).我是xamarin平台的新手,任何人都可以帮助我在Xamarin绑定项目中将以下.h文件转换为“ 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

解决方法:

无法为您编写代码,但是下面的示例是一般模式,您可以在下面的指南中了解更多信息.您面临的主要挑战之一是要确保激发代理上的回调.要对此进行映射,请查看“绑定协议”部分.

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();
}

To complete this binding, you should add the native library to the
project. You can do this by adding the native library to your project,
either by dragging and dropping the native library from Finder onto
the project in the solution explorer, or by right-clicking the project
and choosing Add > Add Files to select the native library. Native
libraries by convention start with the word “lib” and end with the
extension “.a”. When you do this, Xamarin Studio will add two files:
the .a file and an automatically populated C# file that contains
information about what the native library contains:

您将得到一个像这样的文件(libLibraryName.linkwith.cs):

using System;
using MonoTouch.ObjCRuntime;

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

标签:objective-c,xamarin,c
来源: https://codeday.me/bug/20191120/2043330.html