绑定到Objective C库(VFR Reader)

时间:2012-11-04 16:22:41

标签: c# objective-c xamarin.ios monodevelop

我是MonoTouch开发的新手,我想在我的应用中嵌入一些PDF查看功能。我已经找到了几个这样做的资源,但是,我也看到了关于所有其他实现的足够的评论,以使其稳定和快速。

我现在看到有一个很好的ObjectiveC库已经实现了很多功能(CATiledLayer,多线程,页面滚动,缩略图,设备轮换......):https://github.com/vfr/Reader

最后几天,在阅读了monotoch绑定文档后,我试图在MonoTouch中绑定它,但没有成功。 我可以将它导出到库(.a)文件,我已经创建了一个绑定API。

    //@interface ReaderDocument : NSObject <NSObject, NSCoding>
    [BaseType (typeof (NSObject))]
    interface ReaderDocument {

    //- (id)initWithFilePath:(NSString *)fullFilePath password:(NSString *)phrase;
    [Export("initWithFilePath:password")]
    IntPtr Constructor (string path, string phrase);

    //Properties
    [Export("guid")]
    string Guid { get;}

    [Export("fileDate")]
    NSDate FileDate { get;}

    [Export("lastOpen")]
    NSDate LastOpen { get;set;}

    [Export("fileSize")]
    NSNumber FileSize{ get;}

    [Export("pageCount")]
    NSNumber PageCount { get;}

    [Export("pageNumber")]
    NSNumber PageNumber { get;set;}

    [Export("bookmarks")]
    NSMutableIndexSet Bookmarks { get;}

    [Export("fileName")]
    string FileName { get;}

    [Export("password")]
    string Password { get;}

    [Export("fileURL")]
    NSUrl FileURL { get;}

    //Methods

    //+ (ReaderDocument *)withDocumentFilePath:(NSString *)filename password:(NSString *)phrase;
    [Static, Export("withDocumentFilePath:password")]
    ReaderDocument WithDocumentFilePath(string filename, string phrase);

    //+ (ReaderDocument *)unarchiveFromFileName:(NSString *)filename password:(NSString *)phrase;
    [Static, Export("unarchiveFromFileName:password")]
    ReaderDocument UnarchiveFromFileName(string filename, string phrase);

    //- (void)saveReaderDocument;
    [Export("saveReaderDocument")]
    void SaveReaderDocument();

    //- (void)updateProperties;
    [Export("updateProperties")]
    void updateProperties();
}

我非常不确定关注行btw:

//@interface ReaderDocument : NSObject <NSObject, NSCoding>
    [BaseType (typeof (NSObject))]
    interface ReaderDocument

不确定我是否必须对“”做什么?

我现在可以在MonoTouch中创建以下代码

ReaderDocument doc =  ReaderDocument.withDocumentFilePath("Tamarin.pdf","");

ReaderDocument doc = new ReaderDocument("Tamarin.pdf","yrt");

两者都导致“无法识别的选择器”错误

  2012-11-04 22:15:05.731 PFDTest1[4149:1507] +[ReaderDocument withDocumentFilePath:password]: unrecognized selector sent to class 0x2f7738

[ERROR] FATAL UNHANDLED EXCEPTION: MonoTouch.Foundation.MonoTouchException: Objective-C     exception thrown.  Name: NSInvalidArgumentException Reason: +[ReaderDocument   withDocumentFilePath:password]: unrecognized selector sent to class 0x2f7738
at (wrapper managed-to-native)   MonoTouch.ObjCRuntime.Messaging:IntPtr_objc_msgSend_IntPtr_IntPtr   (intptr,intptr,intptr,intptr)
at VFRBinding4.ReaderDocument.withDocumentFilePath (System.String filename,  System.String phrase) [0x00000] in <filename unknown>:0 
at PFDTest1.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00030] in /Users/matthiasvalcke/Projects/PFDTest1/PFDTest1/AppDelegate.cs:39 
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
at PFDTest1.Application.Main (System.String[] args) [0x00000] in  /Users/matthiasvalcke/Projects/PFDTest1/PFDTest1/Main.cs:17 

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

可能存在其他问题,但构造函数的绑定是错误的,即

//- (id)initWithFilePath:(NSString *)fullFilePath password:(NSString *)phrase;
[Export("initWithFilePath:password")]
void InitWithFilePath(string path, string password);

ObjectiveC init*选择器应绑定为C#构造函数。 E.g。

[Export("initWithFilePath:password")]
IntPtr Constructor (string path, string password);

这应该是您用来创建实例的内容,例如

ReaderDocument doc = new ReaderDocument ("sample.pdf", "");
// ...

答案 1 :(得分:1)

我可能完全错了,但我认为你的选择者错了:

e.g。 “withDocumentFilePath:password”应为“withDocumentFilePath:password:”