简体   繁体   English

静态属性未在类中从绑定实例化

[英]Static property not instantiated in class from binding

When I access static property SharedController , the value is null, but the API Definition generated by Objective Sharpie only provides a getter.当我访问静态属性SharedController ,该值为 null,但 Objective Sharpie 生成的 API Definition 仅提供了一个 getter。

BBDeviceController is instantiated, but the SharedController property on it is null. BBDeviceController已实例化,但其上的SharedController属性为空。 Because it only has a getter, I do not believe I am able to instantiate the property in code.因为它只有一个 getter,所以我不相信我能够在代码中实例化该属性。 From what I can tell, SharedController is not being initialized in BBDeviceController 's constructor.据我所知, SharedController没有在BBDeviceController的构造函数中初始化。 I am unsure if BBDeviceController is a static class or just follows the singleton design pattern, but either way, newing up that class is not the solution.我不确定BBDeviceController是静态类还是只是遵循单例设计模式,但无论哪种方式,更新该类都不是解决方案。

BBDeviceController.h BBDeviceController.h

@interface BBDeviceController : NSObject {
    NSObject <BBDeviceControllerDelegate>* delegate;

    BOOL debugLogEnabled;
    BOOL detectAudioDevicePlugged;
}

@property (nonatomic, weak) NSObject <BBDeviceControllerDelegate>* delegate;
@property (nonatomic, getter=isDebugLogEnabled, setter=setDebugLogEnabled:) BOOL debugLogEnabled;
@property (nonatomic, getter=isDetectAudioDevicePlugged, setter=setDetectAudioDevicePlugged:) BOOL detectAudioDevicePlugged;

+ (BBDeviceController *)sharedController;

Resulting API Definition ( APIDefinition.cs )结果 API 定义 ( APIDefinition.cs )

[BaseType(typeof(NSObject))]
[Protocol]
interface BBDeviceController
{
    ...
    // +(BBDeviceController *)sharedController;
    [Static]
    [Export("sharedController")]
    BBDeviceController SharedController { get; }
    ...
}

Call from project's view controller从项目的视图控制器调用

public BBDeviceController bbDeviceController;
public override void ViewDidLoad()
{
    base.ViewDidLoad();
    bbDeviceController = BBDeviceController.SharedController;
    ConnectDevice();
}

private void ConnectDevice()
{
    bbDeviceController.StartBTScan(new string[] { DEVICE_NAME }, 60);
}

From the documentation for the objective C library, you use sharedController like this:从目标 C 库的文档中,您可以像这样使用sharedController

[[BBDeviceController sharedController] setDelegate:self];

When trying to do the C# equivalent, I get an error because SharedController is null.尝试执行 C# 等效项时,我收到错误消息,因为SharedController为 null。 Something else that may be of interest is that the SharedController definition in the resulting API Definitions file was flagged with [Verify(MethodToProperty)] , and I manually had to add [Static] in order to access it from code.可能感兴趣的[Verify(MethodToProperty)]是生成的 API 定义文件中的SharedController定义被标记为[Verify(MethodToProperty)] ,我必须手动添加[Static]以便从代码访问它。

I'm not sure what your exact issue is (it could be it's not linking correctly, it could be the plist settings aren't right, etc) but I have successfully done this with the latest SDK (v3.7.2 as of writing).我不确定您的确切问题是什么(可能是链接不正确,也可能是 plist 设置不正确等)但我已使用最新的 SDK(撰写本文时为 v3.7.2)成功完成此操作. Here are the steps:以下是步骤:

1. Use the following sharpie command to make the bindings: 1.使用以下sharpie命令进行绑定:

sharpie bind --output=. --namespace=WisePad2Binding --sdk=iphoneos13.2 --scope=<path-to-sdk>/BBDevice-iOS-SDK-3.7.2/Library <path-to-sdk>/BBDevice-iOS-SDK-3.7.2/Library/BBDeviceController.h

2. Remove the [Verify] tags 2.去掉[Verify]标签

I went ahead and changed back all the methods changed to properties that were marked by [Verify] just to leave less up to chance.我继续将所有更改为由[Verify]标记的属性的方法改回,只是为了减少机会。

3. Add the libraries to your binding project 3. 将库添加到您的绑定项目

In the binding product, I didn't use the "Native References" folder (well, I tried initially and it did not work).在绑定产品中,我没有使用“Native References”文件夹(好吧,我最初尝试过,但没有用)。 Apparently the "old" way of making a binding project was to just drag the library onto the project root.显然,制作绑定项目的“旧”方法是将库拖到项目根目录上。 When you do this, each library gets a .linkwith.cs attached to it (more on that in a sec).执行此操作时,每个库都会附加一个.linkwith.cs.linkwith.cs将详细介绍)。 The libraries I added were:我添加的库是:

  • libBBDevice-iOS-AudioAndBT-3.7.2.a libBBDevice-iOS-AudioAndBT-3.7.2.a
  • libBBDevice-iOS-AudioAndBT-3.7.2-simulator.a libBBDevice-iOS-AudioAndBT-3.7.2-simulator.a
  • libBBDeviceOTA-iOS-1.5.4.a libBBDeviceOTA-iOS-1.5.4.a
  • libBBDeviceOTA-iOS-1.5.4-simulator.a libBBDeviceOTA-iOS-1.5.4-simulator.a

Then in each .linkwith.cs file change the assembly attribute to be...然后在每个.linkwith.cs文件中将程序集属性更改为...

[assembly: LinkWith ("<library-name>.a", SmartLink = true, ForceLoad = true, Frameworks = "AudioToolbox AVFoundation CoreAudio CoreBluetooth CoreGraphics CoreFoundation CoreLocation ExternalAccessory Foundation MediaPlayer MessageUI QuartzCore SystemConfiguration UIKit", LinkerFlags = "-lc++ -lz -ObjC")]

4. Update your plist 4. 更新你的 plist

Make sure you have all the stuff they list here added to your plist or stuff will not work right.确保您已将此处列出的所有内容添加到您的 plist 中,否则这些内容将无法正常工作。

5. Tips 5. 小贴士

I couldn't get things to work correctly if I didn't do some (all?) things on the main thread, so I just perform all actions on the main thread.如果我没有在主线程上做一些(全部?)事情,我就无法让事情正常工作,所以我只是在主线程上执行所有操作。 Also, don't set the delegate on the shared controller without calling BBDeviceController.SharedController.ReleaseBBDeviceController() (if you had a delegate previously set on there).此外,不要在未调用BBDeviceController.SharedController.ReleaseBBDeviceController()情况下在共享控制器上设置委托(如果您之前在那里设置了委托)。 My thoughts and prayers go out to anybody else trying to use this SDK with Xamarin.我的想法和祈祷传给了其他任何试图将这个 SDK 与 Xamarin 一起使用的人。

When I do something like this, it's for a singleton class.当我做这样的事情时,它是针对单例类的。 https://www.galloway.me.uk/tutorials/singleton-classes/ https://www.galloway.me.uk/tutorials/singleton-classes/

so your sharedController method, should either allocate your BBDeviceController or get it somewhere.所以你的 sharedController 方法,应该分配你的 BBDeviceController 或者把它放在某个地方。 Also, if you can share the library link so we are on the same page.另外,如果您可以共享库链接,那么我们就在同一页面上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM