如何在 Xamarin Android 中使用 V8 JavaScript 引擎

ITh*_*s91 5 v8 xamarin.android xamarin

我有一个 Xamarin Forms 应用程序,我必须在其中调用 JavaScript 并使用其结果。我已经用 iOS 上的 JavaScriptCore 和 Win10 上的 Chakra 解决了这个问题(我假设 ChakraCore 也可以在 Win8.1 上运行),但我喜欢在 android 上使用 V8 JavaScript 引擎。但是我找不到如何在 Xamarin 中使用它。

有没有办法使用它?

Aka*_*ava 1

免责声明:我是作者。

https://github.com/web-atoms/xamarin-v8

添加 NuGet 包

 <PackageReference Include="Xamarin.Android.V8" Version="1.4.79" />
Run Code Online (Sandbox Code Playgroud)

代码

  using(var context = new JSContext( /*Enable Debugging*/ true)) {

    // you can connect to dev tools by visiting url
     // devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9222/backend

  context["printf"] = context.CreateFunction(0, (c, a) => {
      // first parameter is context isself
      // second parameter is an array as IJSValue
        System.Diagnostics.Debug.WriteLine(a[0].ToString());
       return c.Undefined;
  });

   // script location is useful for debugging
   context.Evaluate(scriptText, scriptLocation);

}
Run Code Online (Sandbox Code Playgroud)