针对Xamarin iOS错误的Objective-C库绑定

时间:2017-11-02 16:24:44

标签: ios xamarin.ios cocoapods

我绑定了这个Objective-C库 - ** SPSlideTabBarController,用于我的Xamarin iOS应用程序。

我在ApiDefinitions.cs文件中做了一些更改,当我构建绑定项目时,我遇到了3个构建错误。错误反映在生成的文件SPSlideTabBarProtocol.g.cs

以下是错误:

1. The modifier abstract is not valid for this item

这段代码:

[Export ("initWithTabBarItems:")]
[CompilerGenerated]
public abstract SPSlideTabBarProtocol (SPSlideTabBarItem[] tabBarItems);

第二个错误:

2. cannot declare instance members in a static class  <--- error right on this line

[CompilerGenerated]
    public virtual SPSlideTabBarController SlideTabBarController {
        [Export ("slideTabBarController", ArgumentSemantic.Retain)]
        get {
            SPSlideTabBarController ret;
            if (IsDirectBinding) {
                ret =  Runtime.GetNSObject<SPSlideTabBarController> (global::ApiDefinition.Messaging.IntPtr_objc_msgSend (this.Handle, Selector.GetHandle ("slideTabBarController")));
            } else {
                ret =  Runtime.GetNSObject<SPSlideTabBarController> (global::ApiDefinition.Messaging.IntPtr_objc_msgSendSuper (this.SuperHandle, Selector.GetHandle ("slideTabBarController")));
            }
            return ret;
        }

    }

第3次错误:

3.cannot declare instance members in a static class

[CompilerGenerated]
    public virtual SPSlideTabBarItem SlideTabBarItem { // <--- error right on this line
        [Export ("slideTabBarItem", ArgumentSemantic.Retain)]
        get {
            SPSlideTabBarItem ret;
            if (IsDirectBinding) {
                ret =  Runtime.GetNSObject<SPSlideTabBarItem> (global::ApiDefinition.Messaging.IntPtr_objc_msgSend (this.Handle, Selector.GetHandle ("slideTabBarItem")));
            } else {
                ret =  Runtime.GetNSObject<SPSlideTabBarItem> (global::ApiDefinition.Messaging.IntPtr_objc_msgSendSuper (this.SuperHandle, Selector.GetHandle ("slideTabBarItem")));
            }
            return ret;
        }

ApiDefinition.cs文件中的相应定义是:

1.// @protocol SPSlideTabBarProtocol <NSObject>
[Protocol, Model]
[BaseType(typeof(NSObject))]
//[Model][Protocol]
interface SPSlideTabBarProtocol
{
    // @required -(instancetype)initWithTabBarItems:(NSArray<SPSlideTabBarItem *> *)tabBarItems;
    [Abstract]
    [Export("initWithTabBarItems:")]
    IntPtr Constructor(SPSlideTabBarItem[] tabBarItems);
}

2. // @interface SPSlideTabBarItem (UIViewController)

[BaseType(typeof(UIViewController))]
[Category]
interface UIViewController_SPSlideTabBarItem
{
    // @property (nonatomic, strong) SPSlideTabBarItem * _Null_unspecified slideTabBarItem;
    [Export("slideTabBarItem", ArgumentSemantic.Strong)]
    SPSlideTabBarItem SlideTabBarItem { get; set; }

    // @property (readonly, nonatomic, strong) SPSlideTabBarController * _Nullable slideTabBarController;
    [NullAllowed, Export("slideTabBarController", ArgumentSemantic.Strong)]
    SPSlideTabBarController SlideTabBarController { get; }
}

我尝试了一些解决方案无济于事。有人可以帮我理解这些错误吗?

感谢。

2 个答案:

答案 0 :(得分:0)

我已经能够通过以下方式解决这些错误:

1.The modifier abstract is not valid for this item

我必须做的就是从定义中删除抽象属性,如下所示:

由此:

interface SPSlideTabBarProtocol
{
// @required -(instancetype)initWithTabBarItems:
(NSArray<SPSlideTabBarItem *> *)tabBarItems;
[Abstract]
[Export("initWithTabBarItems:")]
IntPtr Constructor(SPSlideTabBarItem[] tabBarItems);

}

对此:

interface SPSlideTabBarProtocol
{
// @required -(instancetype)initWithTabBarItems:(NSArray<SPSlideTabBarItem *> *)tabBarItems;
[Export("initWithTabBarItems:")]
IntPtr Constructor(SPSlideTabBarItem[] tabBarItems);

}

对于其他两个错误,错误是由于该类是静态的,但它的成员不是。因此,如果该类已被定义为静态,那么我们还需要将成员定义为静态。 以下是变化:

我从属性中添加了静态属性:

  [BaseType(typeof(UIViewController))]
  [Category]
  interface UIViewController_SPSlideTabBarItem
  {
// @property (nonatomic, strong) SPSlideTabBarItem * _Null_unspecified slideTabBarItem;
[Export("slideTabBarItem", ArgumentSemantic.Strong)]
SPSlideTabBarItem SlideTabBarItem { get; set; }

// @property (readonly, nonatomic, strong) SPSlideTabBarController * _Nullable slideTabBarController;
[NullAllowed, Export("slideTabBarController", ArgumentSemantic.Strong)]
SPSlideTabBarController SlideTabBarController { get; }
 }

对此:

 [BaseType(typeof(UIViewController))]
[Category]
interface UIViewController_SPSlideTabBarItem
{
    // @property (nonatomic, strong) SPSlideTabBarItem * _Null_unspecified slideTabBarItem;
    [Static] //<----static attribute added here
    [Export("slideTabBarItem", ArgumentSemantic.Strong)]
    SPSlideTabBarItem SlideTabBarItem { get; set; }

    // @property (readonly, nonatomic, strong) SPSlideTabBarController * _Nullable slideTabBarController;
    [Static] //<----static attribute added here
    [NullAllowed, Export("slideTabBarController", ArgumentSemantic.Strong)]
    SPSlideTabBarController SlideTabBarController { get; }
}

答案 1 :(得分:0)

对于2,你应该改为:

// @interface SPSlideTabBarItem (UIViewController)
[Category]
[BaseType (typeof(UIViewController))]
interface UIViewController_SPSlideTabBarItem
{
    // @property (nonatomic, strong) SPSlideTabBarItem * _Null_unspecified slideTabBarItem;
    [Export ("slideTabBarItem")]
    SPSlideTabBarItem Get_SlideTabBarItem();

    [Export ("setslideTabBarItem:")]
    void Set_SPSlideTabBarItem(SPSlideTabBarItem value);


    // @property (readonly, nonatomic, strong) SPSlideTabBarController * _Nullable slideTabBarController;
    [Export ("slideTabBarController")]
    SPSlideTabBarController Get_SlideTabBarController()
}

这是因为C#只支持扩展方法,但ObjC中有扩展属性。