我有另一个与绑定相关的问题(绑定问题:在绑定类调用基础方法调用覆盖方法。导致无限递归),它已经变得难以处理,所以我想我会从一个包含最好的新问题开始我目前知道的信息。
我正在尝试在 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
,但是查看这个方法的源代码(可在此处获得),它从不尝试调用自身或子类实现。相反,它在内部调用supportsOrientation:
,它检查一个数组以查看是否支持此方向。
无论如何,我知道supportsOrientation:
最终会调用它,我也尝试从它调用它ShouldAutoRotateToInterfaceOrientation
,但后来我只是在该方法中得到了奇怪的回调行为。
如果我直接实例化CDVViewController
,那么一切似乎都按预期工作,但我需要为我的项目重写一些方法。当我扩展CDVViewController
时,这种行为开始发生。
我对此感到很困惑,我无法弄清楚我做错了什么,所以任何帮助将不胜感激。
更新 #1根据 Rolf 的要求,这里是生成的类。我希望这就是你要找的。出于空间原因,我将其添加为 Gist 此处https://gist.github.com/innopal/5288047