编辑:我在我的博客上写了这个。当我详细介绍 Cordova 的各个部分如何工作时,这有点冗长。在这里查看。
所以我在过去的一天里一直在做这个,在 MonoDroid中肯定是可能的。我不明白为什么同样的原则不适用于 MonoTouch。
你有 MonoDroid 在 PhoneGap 上运行吗?好的。大概您已经为 Cordova 创建了 MonoDroid 绑定。
你会发现你可以访问CordovaPlugin
,PluginEntry
和PluginManager
类。但是,有些事情不会像CordovaInterface
一些接受org.json.*
类型的方法。我们可以使用 JNI 来解决这个问题,或者让它保持原样并让它工作。
基DroidGap
类 implements CordovaInterface
,所以我们将使用它。您将调用 base.LoadUrl(Config.StartUrl);
,此时将发生两件重要的事情:
- 您将能够
CordovaWebView
使用属性访问实例AppView
。
- 您还可以
PluginManager
使用AppView.PluginManager
.
您需要创建自定义PluginEntry
子类和自定义CordovaPlugin
子类。如果您没有CordovaInterface
正确映射 type 和 org.json.* 包,则必须采取一些捷径。但这对我有用。
public override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
base.LoadUrl(Config.StartUrl);
var plugin = new SamplePluginEntry("Sample");
AppView.PluginManager.AddService(plugin);
plugin.CreatePlugin(AppView, this);
}
public class SamplePluginEntry : PluginEntry
{
public SamplePluginEntry(String service) : base(service, "UNUSED.CLASSNAME", false)
{
/* we'll pass klass but we'll initialize the plugin ourselves so that Cordova
* doesn't try to find the class name that doesn't exist */
}
public CordovaPlugin CreatePlugin(CordovaWebView webView, DroidGap context)
{
var t = new SamplePlugin();
t.InitializePlugin(webView, context);
base.Plugin = t;
return Plugin;
}
}
public class SamplePlugin : CordovaPlugin
{
private CordovaWebView webView;
private DroidGap context;
public void InitializePlugin(CordovaWebView webView, DroidGap context)
{
this.webView = webView;
this.context = context;
}
public override bool Execute(String action, String rawArgs, CallbackContext callbackContext)
{
/* This code will execute */
return false;
}
}