Monotouch/Xamarin 绑定继承 [英] Monotouch/Xamarin Binding Inheritance

查看:18
本文介绍了Monotouch/Xamarin 绑定继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 FastPDFKit 实现单点绑定.我在继承构造函数时遇到了麻烦.我正在尝试从 fastPDFKit 绑定ReaderViewController".ReaderViewController 继承自 MFDocumentViewController,MFDocumentViewController 继承自 UIViewController.

I'm trying to implement a monotouch binding for FastPDFKit. I'm having trouble with an inherited constructor. I'm trying to bind "ReaderViewController" from fastPDFKit. ReaderViewController inherits from MFDocumentViewController which inherits from 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 时出现两个错误:

This code does not build, giving me two errors when I make the new 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 接口中来消除错误.

Now, I can get rid of the errors by taking the binding Exports from MFDocumentViewController and putting them in my ReaderViewController Interface.

    [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 中定义的.如何获得绑定以正确使用这些继承的方法/构造函数.

But I don't want to do this, because those constructors/methods are defined in MFDocumentViewController. How can I get the binding to correctly use those inherited methods/constructors.

推荐答案

您的修复是正确的实现.

Your fix is the correct implementation.

.NET(可能还有所有 OO 语言)中的 ctor 继承需要定义基本 ctor.让我举个例子.

Ctor inheritance in .NET (and probably all OO languages) require the base ctor to be defined. Let me give an example.

这很好用

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 与参数一起执行.

When you do new C("hello"), the ctor for A, then B, then C is executed with the parameter.

这不起作用:

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.

The reason is the compiler does have to call the B ctor (as C inherit from it) but doesn't know which ctor to use.

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

So, when binding an obj-C library for monotouch, make sure you re-declare all the constructors that might need to be called at some point.

这篇关于Monotouch/Xamarin 绑定继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆