gpt4 book ai didi

c# - xamarin 绑定(bind) objective-c 库

转载 作者:搜寻专家 更新时间:2023-10-30 20:08:50 24 4
gpt4 key购买 nike

我想在 xamarin 中绑定(bind)一个 objective-c 库(用于使用电缆)。我是 xamarin 平台的新手,谁能帮我将下面的 .h 文件转换为 Xamarin 绑定(bind)项目中的“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

最佳答案

无法为您编写,但下面的示例是一般模式,您可以在下面链接的指南中了解更多信息。您的主要挑战之一是确保触发委托(delegate)的回调。要对此进行映射,请查看“绑定(bind)协议(protocol)”部分。

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

API定义.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)]

关于c# - xamarin 绑定(bind) objective-c 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30118083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com