【问题标题】:Xamarin Forms - use iOS native libraryXamarin Forms - 使用 iOS 原生库
【发布时间】:2019-12-01 13:52:46
【问题描述】:

我有使用 Xamarin Forms 的移动应用程序。我还有用 Objective-C 编写的库 something.framework 和 XCode 中的示例项目。

我需要在跨平台应用程序中使用这个库。我在Xamarin.iOS 项目中添加了对这个库的本地引用。我使用依赖接口来管理从 Xamarin Forms 到 Xamarin-iOS 的按钮操作。问题是 Visual Studio 没有将本机引用与我在课堂上的代码连接起来。使用指令不访问本机库,因此当我尝试使用方法表单库时出现很多错误。我已经阅读了微软关于绑定本机库的说明,但没有给出答案。

如何解决?

以下是我使用 Objective Sharpie 时来自终端的日志:

Johns-Mac-mini:~ johnmiller$ sharpie bind
-output=InfColorPickerCustom -namespace=InfColorPickerCustom ~/Desktop/InfColorPicker/InfColorPicker/*.h -sdk=iphoneos12.1 Parsing 8 header files... In file included from /var/folders/_g/mb3qv73j16d_mdwzb8bf5hww0000gn/T/tmp765fc04f.h:2: /Users/johnmiller/Desktop/InfColorPicker/InfColorPicker/InfColorIndicatorView.h:19:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute] @property (nonatomic) UIColor* color; ^ /Users/johnmiller/Desktop/InfColorPicker/InfColorPicker/InfColorIndicatorView.h:19:1: warning: default property attribute 'assign' not appropriate for non-GC object [-Wobjc-property-no-attribute] In file included from /var/folders/_g/mb3qv73j16d_mdwzb8bf5hww0000gn/T/tmp765fc04f.h:3: In file included from /Users/johnmiller/Desktop/InfColorPicker/InfColorPicker/InfColorPicker.h:60: /Users/johnmiller/Desktop/InfColorPicker/InfColorPicker/InfColorPickerController.h:28:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute] @property (nonatomic) UIColor* sourceColor; ^ /Users/johnmiller/Desktop/InfColorPicker/InfColorPicker/InfColorPickerController.h:28:1: warning: default property attribute 'assign' not appropriate for non-GC object [-Wobjc-property-no-attribute] /Users/johnmiller/Desktop/InfColorPicker/InfColorPicker/InfColorPickerController.h:29:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [-Wobjc-property-no-attribute] @property (nonatomic) UIColor* resultColor; ^ /Users/johnmiller/Desktop/InfColorPicker/InfColorPicker/InfColorPickerController.h:29:1: warning: default property attribute 'assign' not appropriate for non-GC object [-Wobjc-property-no-attribute]

Binding...   [write] ApiDefinitions.cs   [write] StructsAndEnums.cs

Binding Analysis:   Automated binding is complete, but there are a few APIs which have been flagged with [Verify] attributes. While the entire binding should be audited for best API design practices, look more closely at APIs with the following Verify attribute hints:

  ConstantsInterfaceAssociation (200 instances):
    There's no foolproof way to determine with which Objective-C interface an extern variable declaration may be associated. Instances of these are bound as [Field] properties in a partial interface into a nearby concrete interface to produce a more intuitive API,
    possibly eliminating the 'Constants' interface altogether.

  MethodToProperty (193 instances):
    An Objective-C method was bound as a C# property due to convention such as taking no parameters and returning a value (non-void return). Often methods like these should be bound as properties to surface a nicer API, but sometimes false-positives can occur and
    the binding should actually be a method.

  StronglyTypedNSArray (42 instances):
    A native NSArray* was bound as NSObject[]. It might be possible to more strongly type the array in the binding based on expectations set through API documentation (e.g. comments in the header file) or by examining the array contents through testing. For example,
     an NSArray* containing only NSNumber* instances can be bound as NSNumber[] instead of NSObject[].

  PlatformInvoke (3256 instances):
    In general P/Invoke bindings are not as correct or complete as Objective-C bindings (at least currently). You may need to fix up the library name (it defaults to '__Internal') and return/parameter types manually to conform to C calling conventionsfor the target
    platform. You may find you don't even want to expose the C API in your binding, but if you do, you'll probably also want to relocate the definition to a more appropriate class and expose a stronger type-safe wrapper. For P/Invoke guidance, see http://www.mono-
    project.com/docs/advanced/pinvoke/.

  InferredFromMemberPrefix (29 instances):
    The name of this originally anonymous declaration was taken from a common prefix of its members.

  Once you have verified a Verify attribute, you should remove it from the binding source code. The presence of Verify attributes intentionally cause build failures.
     For more information about the Verify attribute hints above, consult the Objective Sharpie documentation by running 'sharpie docs' or visiting the following URL:

    http://xmn.io/sharpie-docs 6 warnings generated.

Done. Johns-Mac-mini:~ johnmiller$

【问题讨论】:

  • 您是否为 ObjC 框架创建了 C# 绑定?只需添加对框架的本机引用即可创建绑定,您要么必须手动创建它们,要么使用 Sharpie 引导这些绑定。
  • 以防万一您在 Cocoapod 中提供了该框架,那么它就更容易了。 docs.microsoft.com/en-us/xamarin/cross-platform/macios/binding/…
  • 您是否参考过此文档(docs.microsoft.com/en-us/xamarin/cross-platform/macios/binding/… 显示一些日志信息或图像会有所帮助。
  • 我已经使用 Sharpie 从终端生成了 ApiDefinition.cs 和 StructsAndEnums.cs,但它们有超过 60 000 行和 11 000 多个错误。如何避免这些冲突?
  • @YoungEddie Okey,首先需要分析出现的错误日志,然后才能找到原因。

标签: xamarin


【解决方案1】:

我已经解决了。使用 Objective Sharpie 时需要使用范围。简单添加“-scope ~/Desktop/InfColorPicker/InfColorPicker”。然后 Sharpie 正在生成小文件。

【讨论】:

    最近更新 更多