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

问题描述 投票:5回答:1

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

有什么方法可以使用它吗?

xamarin xamarin.android v8
1个回答
0
投票

声明:我是作者。

https:/github.comweb-atomsxamarin-v8。

添加NuGet包

 <PackageReference Include="Xamarin.Android.V8" Version="1.4.79" />

编码

  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);

}
© www.soinside.com 2019 - 2024. All rights reserved.