绑定问题:扩展类引导方法调用自身?

时间:2013-04-01 19:14:56

标签: xamarin.ios xamarin

我还有另一个与绑定有关的问题(Issues with Bindings: Calling base method on binding class calls override method. Leads to infinite recursion),而且它已经变得笨拙了,所以我想我会重新提出一个新问题,其中包含我目前所知的最佳信息。

我正在尝试在我正在MonoTouch中工作的项目中使用Cordova。我一直在研究Cordova的绑定。

这是我对Cordova 2.4.0类的绑定(可在此处https://github.com/apache/cordova-ios/tree/2.4.0/CordovaLib/Classes):( 注意这不是一个完整的绑定,只是到目前为止我需要的部分

interface CDVScreenOrientationDelegate {
    [Export ("supportedInterfaceOrientations")]
    uint SupportedInterfaceOrientations ();

    [Export ("shouldAutorotateToInterfaceOrientation:")]
    bool ShouldAutoRotateToInterfaceOrientation (UIInterfaceOrientation interfaceOrientation);

    [Export ("shouldAutorotate")]
    bool ShouldAutoRotate ();
}

[BaseType (typeof (UIViewController))]
interface CDVViewController : CDVScreenOrientationDelegate {
    [Export ("webView")]
    UIWebView WebView { get; set; }

    [Export ("pluginObjects")]
    NSMutableDictionary PluginObjects { get; }

    [Export ("pluginsMap")]
    NSDictionary PluginsMap { get; }

    [Export ("settings")]
    NSDictionary Settings { get; }

    [Export ("whitelist")]
    CDVWhitelist Whitelist { get; }

    [Export ("loadFromString")]
    bool LoadFromString { get; }

    [Export ("useSplashScreen")]
    bool UseSplashScreen { get; set; }

    [Export ("activityView")]
    UIActivityIndicatorView ActivityView { get; }

    [Export ("imageView")]
    UIImageView ImageView { get; }

    [Export ("wwwFolderName")]
    string WwwFolderName { get; set; }

    [Export ("startPage")]
    string StartPage { get; set; }

    [Export ("commandQueue")]
    NSObject CommandQueue { get; set; }

    [Export ("commandDelegate")]
    NSObject CommandDelegate { get; set; }

    [Export ("userAgent")]
    string UserAgent { get; }

    [Export ("printMultitaskingInfo")]
    void PrintMultitaskingInfo ();

    [Export ("createGapView")]
    void CreateGapView ();

    [Export ("newCordovaViewWithFrame:")]
    UIWebView NewCordovaView(RectangleF bounds);

    [Export ("javascriptAlert:")]
    void JavascriptAlert (string text);

    [Export ("appURLScheme")]
    string AppUrlScheme ();

    [Export ("parseInterfaceOrientations:")]
    NSArray ParseInterfaceOrientations (NSArray orientations);

    [Export ("supportsOrientation:")]
    bool SupportsOrientation (UIInterfaceOrientation orientation);

    [Export ("getCommandInstance:")]
    NSObject GetCommandInstance (string pluginName);

    [Export ("registerPlugin:withClassName:")]
    void RegisterPluginWithClassName (CDVPlugin plugin, string className);

    [Export ("URLisAllowed:")]
    bool UrlIsAllowed (NSUrl url);

    [Static] [Export ("getBundlePlist:")]
    NSDictionary GetBundlePlist (string plistName);

    [Static] [Export ("applicationDocumentsDirectory")]
    string ApplicationDocumentsDirectory ();

    // The following methods and properties come from UIWebViewDelegate, but we can't do multiple inheritance
    //[Export ("webView:shouldStartLoadWithRequest:navigationType:")]
    //bool ShouldStartLoad (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType);
}

[BaseType (typeof(NSObject))]
interface CDVWhitelist {
    [Export ("whitelist")]
    NSArray Whitelist { get; }

    [Export ("expandedWhitelist")]
    NSArray ExpandedWhitelist { get; }

    [Export ("allowAll")]
    bool AllowAll { get; }

    [Export ("initWithArray:")]
    IntPtr Constructor (NSArray array);

    [Export ("URLIsAllowed:")]
    bool UrlIsAllowed (NSUrl url);

    [Export ("schemeIsAllowed:")]
    bool SchemeIsAllowed (string scheme);

    [Export ("errorStringForURL:")]
    string ErrorStringForUrl (NSUrl url);
}

[BaseType (typeof(NSObject))]
interface CDVPlugin {
    [Export ("webView")]
    UIWebView WebView { get; set; }

    [Export ("viewController")]
    UIViewController ViewController { get; set; }

    [Export ("commandDelegate")]
    NSObject CommandDelegate { get; set; }

    [Export ("hasPendingOperation")]
    bool HasPendingOperation { get; }

    [Export ("initWithWebView:")]
    CDVPlugin InitWithWebView (UIWebView theWebView);

    [Export ("handleOpenURL:")]
    void HandleOpenUrl (NSNotification notification);

    [Export ("onAppTerminate")]
    void OnAppTerminate ();

    [Export ("onMemoryWarning")]
    void OnMemoryWarning ();

    [Export ("onReset")]
    void OnReset ();

    [Export ("appDelegate")]
    NSObject AppDelegate ();
}

这是使用来自Monotouch Binding Syntax For Protocols的miguel.de.icaza协议。这没有CDVViewController采用的UIWebViewDelegate

@interface CDVViewController : UIViewController <UIWebViewDelegate, CDVScreenOrientationDelegate>{
    @protected
    CDVCommandDelegateImpl* _commandDelegate;
    @protected
    CDVCommandQueue* _commandQueue; 
    NSString* _userAgent;
}

或者,我发现Rolf Bjarne Kvinge在http://monotouch.2284126.n4.nabble.com/Objective-C-protocol-binding-method-not-invoked-td4105828.html推荐采用Adopts语法。不幸的是,我无法实现这一点,因为当我尝试构建绑定时,它会抱怨无法使System.Type符合String。但这是一个不同的问题。

当我尝试子类化CDVViewController时,我遇到了其他问题。特别是在调用ViewDidLoad之后,系统会调用我的子类“ShouldAutoRotateToInterfaceOrientation

为了证实这一点,我做了以下事情:

public class WebViewController : CDVViewController
{
    public override bool ShouldAutoRotateToInterfaceOrientation (MonoTouch.UIKit.UIInterfaceOrientation interfaceOrientation)
    {
        Console.WriteLine ("Enter ShouldAutoRotateToInterfaceOrientation");
        var output = base.ShouldAutoRotateToInterfaceOrientation;
        Console.WriteLine ("Leave ShouldAutoRotateToInterfaceOrientation");
        return output;
    }
}

我在ShouldAutoRotateToInterfaceOrientation中放了一个断点,当我走进这个方法时,当我到达base.ShouldAutoRotateToInterfaceOrientation时,它只是跳回到这个方法的开头,而我的调用栈已经现在添加了Cordova.CDVViewController.ShouldAutoRotateToInterfaceOrientation,但查看此方法的源代码(可用here),它从不尝试调用自身或子类实现。相反,它会在内部调用supportsOrientation:,它会检查数组以查看是否支持此方向。

最后我知道supportsOrientation:被调用了,我也尝试从ShouldAutoRotateToInterfaceOrientation调用它,但后来我只是在该方法中调用了自己的怪异回调行为。

如果我直接实例化CDVViewController,那么一切似乎都按预期工作,但我需要覆盖我的项目的一些方法。当我延长CDVViewController时, 然后这种行为开始发生。

我对此感到很困惑,我无法弄清楚我做错了什么,所以任何帮助都会非常感激。

更新#1 根据Rolf的请求,以下是生成的类。我希望这就是你要找的东西。出于空间原因,我在此处将其添加为Gist https://gist.github.com/innopal/5288047

1 个答案:

答案 0 :(得分:0)

正如Rolf所指出的,这个问题的解决方案是我在论证中加入了-e标志。

正如btouch的帮助中所述,

-e  Generates smaller classes that cannot be subclassed (previously called 'external mode').

所以这里的问题是我在不知不觉中使用了一个专门使类不可分类的标志,然后尝试将它们子类化。