Monotouch / Xamarin绑定inheritance
我试图为FastPDFKit实现一个monotouch绑定。 我遇到了一个inheritance的构造函数的问题。 我试图从fastPDFKit绑定“ReaderViewController”。 ReaderViewControllerinheritance自从UIViewControllerinheritance的MFDocumentViewController。
我的C#
NSUrl fullURL = new NSUrl (fullPath); FastPDFKitBinding.MFDocumentManager DocManager = new FastPDFKitBinding.MFDocumentManager (fullURL); DocManager.EmptyCache (); //line where the errors occur FastPDFKitBinding.ReaderViewController pdfView = new FastPDFKitBinding.ReaderViewController (DocManager); pdfView.DocumentID = PageID.ToString (); Source.PView.PresentViewController(pdfView, true, null);
此代码不会生成,当我使新的ReaderViewController时给我两个错误:
Error CS1502: The best overloaded method match for `FastPDFKitBinding.ReaderViewController.ReaderViewController(MonoTouch.Foundation.NSCoder)' has some invalid arguments (CS1502) (iOSFlightOpsMobile) Error CS1503: Argument `#1' cannot convert `FastPDFKitBinding.MFDocumentManager' expression to type `MonoTouch.Foundation.NSCoder' (CS1503) (iOSFlightOpsMobile)
我的约束力的相关部分
namespace FastPDFKitBinding { [BaseType (typeof (UIAlertViewDelegate))] interface MFDocumentManager { [Export ("initWithFileUrl:")] IntPtr Constructor (NSUrl URL); [Export ("emptyCache")] void EmptyCache (); [Export ("release")] void Release (); } [BaseType (typeof (UIViewController))] interface MFDocumentViewController { [Export ("initWithDocumentManager:")] IntPtr Constructor (MFDocumentManager docManager); [Export ("documentId")] string DocumentID { get; set; } [Export ("documentDelegate")] NSObject DocumentDelegate { set; } } [BaseType (typeof (MFDocumentViewController))] interface ReaderViewController { } }
现在,我可以通过从MFDocumentViewController获取绑定导出并将其放入我的ReaderViewController接口中来消除这些错误。
[BaseType (typeof (UIViewController))] interface MFDocumentViewController { } [BaseType (typeof (MFDocumentViewController))] interface ReaderViewController { [Export ("initWithDocumentManager:")] IntPtr Constructor (MFDocumentManager docManager); [Export ("documentId")] string DocumentID { get; set; } [Export ("documentDelegate")] NSObject DocumentDelegate { set; } }
但我不想这样做,因为这些构造函数/方法在MFDocumentViewController中定义。 我怎样才能得到绑定正确使用这些inheritance的方法/构造函数。
你的修复是正确的实现。
.NET中的Ctorinheritance(也可能是所有的OO语言)都需要定义基本ctor。 让我举个例子。
这工作正常
class A { public A (string m) {} } class B : A{ public B (string m) : base (m) {} } class C : B { public C (string m) : base (m) {} }
当你做new C("hello")
,A的Ctor,B,然后C是用参数执行的。
这不起作用:
class A { public A (string m) {} } class B : A { public B () : base ("empty") {} } class C : B { public C (string m) : base (m) {} }
原因是编译器不得不调用B ctor(因为Cinheritance它),但不知道使用哪个ctor。
所以,在绑定一个obj-C库的monotouch时,确保你重新声明了所有可能需要被调用的构造函数。