編集:これをブログに書きました。Cordova の各部分がどのように機能するかを詳しく説明するので、少し長くなります。ここで確認してください。
それで、私はこれに取り組んできましたが、MonoDroid では確かに可能です。同じ原則が MonoTouch に適用されない理由がわかりません。
PhoneGap で MonoDroid を実行していますか? 良い。おそらく、Cordova 用の MonoDroid バインディングが作成されているはずです。
CordovaPlugin
、PluginEntry
およびPluginManager
クラスにアクセスできることがわかります。ただし、いくつかのことは似CordovaInterface
ておらず、型を受け取るいくつかのメソッドがありorg.json.*
ます。JNI を使用してこれを回避するか、そのままにして動作させることができます。
DroidGap
基本クラスは を実装しCordovaInterface
ているので、それを使用します。を呼び出すと base.LoadUrl(Config.StartUrl);
、次の 2 つの重要なことが起こります。
CordovaWebView
プロパティを使用してインスタンスにアクセスできますAppView
。
PluginManager
を使用して への参照を取得することもできますAppView.PluginManager
。
PluginEntry
カスタムサブクラスとカスタム サブクラスを作成する必要がありますCordovaPlugin
。CordovaInterface
タイプと 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;
}
}