Xamarin绑定对象c库的问题

时间:2013-10-28 21:17:24

标签: c# objective-c binding xamarin.ios static-linking

我想绑定那个库

https://github.com/Appsido/AOTag?source=cc

静态库它是由开发人员提供的。我生成了我的Binding项目和API定义

using System.Drawing;
using System;
using MonoTouch.ObjCRuntime;
using MonoTouch.UIKit;
using MonoTouch.Foundation;

namespace AOTTagBinding {

    [BaseType (typeof (NSObject))]
    public partial interface EGOImageLoader {

        [Static, Export ("SharedImageLoader")]
        EGOImageLoader SharedImageLoader { get; }

        [Export ("IsLoadingImageURL")]
        bool IsLoadingImageURL (NSUrl aURL);

        [Export ("LoadImageForURL")]
        void LoadImageForURL (NSUrl aURL, EGOImageLoaderObserver observer);

        [Export ("ImageForURL")]
        UIImage ImageForURL (NSUrl aURL, EGOImageLoaderObserver observer);

        [Export ("RemoveObserver")]
        void RemoveObserver (EGOImageLoaderObserver observer);

        [Export ("RemoveObserver")]
        void RemoveObserver (EGOImageLoaderObserver observer, NSUrl aURL);

        [Export ("HasLoadedImageURL")]
        bool HasLoadedImageURL (NSUrl aURL);

        [Export ("CancelLoadForURL")]
        void CancelLoadForURL (NSUrl aURL);

        [Export ("ClearCacheForURL")]
        void ClearCacheForURL (NSUrl aURL);

        [Export ("ClearCacheForURL")]
        void ClearCacheForURL (NSUrl aURL, string style);

        [Export ("currentConnections", ArgumentSemantic.Retain)]
        NSDictionary CurrentConnections { get; set; }
    }

    [BaseType (typeof (NSObject))]
    [Model]
    public partial interface EGOImageLoaderObserver {

        [Export ("ImageLoaderDidLoad")]
        void ImageLoaderDidLoad (NSNotification notification);

        [Export ("ImageLoaderDidFailToLoad")]
        void ImageLoaderDidFailToLoad (NSNotification notification);
    }

    [BaseType (typeof (UIImageView))]
    public partial interface EGOImageView : EGOImageLoaderObserver {

        [Export ("InitWithPlaceholderImage")]
        IntPtr Constructor (UIImage anImage);

        [Export ("InitWithPlaceholderImage")]
        IntPtr Constructor (UIImage anImage, EGOImageViewDelegate aDelegate);

        [Export ("CancelImageLoad")]
        void CancelImageLoad ();

        [Export ("imageURL", ArgumentSemantic.Retain)]
        NSUrl ImageURL { get; set; }

        [Export ("placeholderImage", ArgumentSemantic.Retain)]
        UIImage PlaceholderImage { get; set; }

        [Export ("delegate", ArgumentSemantic.Assign)]
        EGOImageViewDelegate Delegate { get; set; }
    }

    [Model, BaseType (typeof (NSObject))]
    public partial interface EGOImageViewDelegate {

        [Export ("ImageViewLoadedImage")]
        void ImageViewLoadedImage (EGOImageView imageView);

        [Export ("ImageViewFailedToLoadImage")]
        void ImageViewFailedToLoadImage (EGOImageView imageView, NSError error);
    }

    [BaseType (typeof (NSObject))]
    [Model]
    public partial interface AOTagDelegate {

        [Export ("TagDistantImageDidLoad")]
        void TagDistantImageDidLoad (AOTag tag);

        [Export ("TagDistantImageDidFailLoad")]
        void TagDistantImageDidFailLoad (AOTag tag, NSError error);

        [Export ("TagDidAddTag")]
        void TagDidAddTag (AOTag tag);

        [Export ("TagDidRemoveTag")]
        void TagDidRemoveTag (AOTag tag);

        [Export ("TagDidSelectTag")]
        void TagDidSelectTag (AOTag tag);
    }

    [BaseType (typeof (UIView))]
    public partial interface AOTagList {

        [Export ("delegate", ArgumentSemantic.Assign)]
        AOTagDelegate Delegate { get; set; }

        [Export ("tags", ArgumentSemantic.Retain)]
        NSMutableArray Tags { get; set; }

        [Export ("AddTag")]
        void AddTag (string tTitle, string tImage);

        [Export ("AddTag")]
        void AddTag (string tTitle, string tImage, UIColor labelColor, UIColor backgroundColor, UIColor closeColor);

        [Export ("AddTag")]
        void AddTag (string tTitle, NSUrl imageURL, string tPlaceholderImage);

        [Export ("AddTag")]
        void AddTag (string tTitle, string tPlaceholderImage, NSUrl imageURL, UIColor labelColor, UIColor backgroundColor, UIColor closeColor);

        [Export ("AddTags")]
        void AddTags (NSObject [] tags);

        [Export ("RemoveTag")]
        void RemoveTag (AOTag tag);

        [Export ("RemoveAllTag")]
        void RemoveAllTag ();
    }

    [BaseType (typeof (UIView))]
    public partial interface AOTag : EGOImageViewDelegate {

        [Export ("delegate", ArgumentSemantic.Assign)]
        AOTagDelegate Delegate { get; set; }

        [Export ("tLabelColor", ArgumentSemantic.Retain)]
        UIColor TLabelColor { get; set; }

        [Export ("tBackgroundColor", ArgumentSemantic.Retain)]
        UIColor TBackgroundColor { get; set; }

        [Export ("tCloseButtonColor", ArgumentSemantic.Retain)]
        UIColor TCloseButtonColor { get; set; }

        [Export ("tImage", ArgumentSemantic.Retain)]
        UIImage TImage { get; set; }

        [Export ("tImageURL", ArgumentSemantic.Retain)]
        EGOImageView TImageURL { get; set; }

        [Export ("tTitle", ArgumentSemantic.Copy)]
        string TTitle { get; set; }

        [Export ("tURL", ArgumentSemantic.Retain)]
        NSUrl TURL { get; set; }

        [Export ("GetTagSize")]
        SizeF GetTagSize { get; }
    }

    [BaseType (typeof (UIView))]
    public partial interface AOTagCloseButton {

        [Export ("cColor", ArgumentSemantic.Retain)]
        UIColor CColor { get; set; }

        [Export ("InitWithFrame")]
        IntPtr Constructor (RectangleF frame, UIColor color);
    }
}

并建立良好。我把我的AOTDelegate命名为:

using System;
using AOTTagBinding;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;
using MonoTouch.Foundation; 

namespace MyPorject
{
    public class TagDelegate: AOTagDelegate
    {
        public TagDelegate(){

        }
        public override void TagDistantImageDidLoad (AOTag tag){

        }

        public override void TagDistantImageDidFailLoad (AOTag tag, NSError error){

        }

        public override void TagDidAddTag (AOTag tag){
        }

        public override void TagDidRemoveTag (AOTag tag){

        }
        public override void TagDidSelectTag (AOTag tag){

        }
    }
}

最后我以这种方式在我的ViewController中使用

AOTagList tags = new AOTagList();
            tags.Frame = new RectangleF(0.0f,
                                        50.0f,
                                        320.0f,
                                        300.0f);

            TagDelegate deleg = new TagDelegate();
            tags.Delegate = deleg;
            tags.RemoveAllTag ();
            tags.Tags = new NSMutableArray ();
            this.View.AddSubview(tags);

但是当我调用RemoveAllTag()

时会有异常
Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[AOTagList RemoveAllTag]: unrecognized selector sent to instance 0x7e61d740

更新 通过响应,我已经对API进行了更新,就像这样

[BaseType (typeof (UIView))]
public partial interface AOTagList {

    [Export ("delegate", ArgumentSemantic.Assign)]
    AOTagDelegate Delegate { get; set; }

    [Export ("tags", ArgumentSemantic.Retain)]
    NSMutableArray Tags { get; set; }

    [Export("addTag:withImage:")]
    void AddTag (string tTitle, string tImage);

    [Export ("addTag")]
    void AddTag (string tTitle, string tImage, UIColor labelColor, UIColor backgroundColor, UIColor closeColor);

    [Export ("addTag")]
    void AddTag (string tTitle, NSUrl imageURL, string tPlaceholderImage);

    [Export ("addTag")]
    void AddTag (string tTitle, string tPlaceholderImage, NSUrl imageURL, UIColor labelColor, UIColor backgroundColor, UIColor closeColor);

    [Export ("addTags")]
    void AddTags (NSObject [] tags);

    [Export ("removeTag")]
    void RemoveTag (AOTag tag);

    [Export ("removeAllTag")]
    void RemoveAllTag ();
}

1 个答案:

答案 0 :(得分:2)

绑定生成器(Objective Sharpie)有一个错误,它不会输出正确的选择器。

这意味着[Export]属性中的所有字符串常量都是错误的。

对于RemoveAllTag方法,这很可能是它应该说的:

[Export ("removeAllTag")]
void RemoveAllTag ();

注意选择器名称中的小写“r”。