【问题标题】:Monotouch/Xamarin Binding InheritanceMonotouch/Xamarin 绑定继承
【发布时间】:2013-11-05 16:56:21
【问题描述】:

我正在尝试为 FastPDFKit 实现单点触控绑定。我在使用继承的构造函数时遇到问题。我正在尝试从 fastPDFKit 绑定“ReaderViewController”。 ReaderViewController 继承自 MFDocumentViewController,MFDocumentViewController 继承自 UIViewController。

我的 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 中定义的。如何让绑定正确使用那些继承的方法/构造函数。

【问题讨论】:

    标签: c# ios xamarin.ios xamarin


    【解决方案1】:

    您的修复是正确的实现。

    .NET(可能还有所有 OO 语言)中的 ctor 继承需要定义基本 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、B、C 的 ctor。

    这不起作用:

    class A {
        public A (string m) {}
    }
    
    class B : A {
        public B () : base ("empty") {}
    }
    
    class C : B {
        public C (string m) : base (m) {}
    }
    

    原因是编译器确实必须调用 B ctor(因为 C 继承自它)但不知道要使用哪个 ctor。

    因此,在为单触式绑定 obj-C 库时,请确保重新声明所有可能需要在某个时候调用的构造函数。

    【讨论】:

    • 感谢 Stephane 提供的示例/说明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2020-10-26
    • 2023-03-04
    相关资源
    最近更新 更多