I have a Xamarin Forms application in which I have to call JavaScript and use its result. I've resolved it with JavaScriptCore on iOS and Chakra on Win10 (and I assume ChakraCore would work on Win8.1 as well), but I like to use the V8 JavaScript Engine on android. However I can't find how I can use it in Xamarin.
Is there even a way to use it?
Summary. By integrating Jint, we can add JavaScript support into our Xamarin. Forms apps.
Chrome V8 is a JavaScript engine, which means that it executes JavaScript code. Originally, JavaScript was written to be executed by web browsers. Chrome V8, or just V8, can execute JavaScript code either within or outside of a browser, which makes server-side scripting possible.
Apps Script supports two JavaScript runtimes: the modern V8 runtime and an older one powered by Mozilla's Rhino JavaScript interpreter. The V8 runtime supports modern ECMAScript syntax and features. The V8 runtime documentation states: You can use modern ECMAScript syntax in scripts that are powered by the V8 runtime.
Disclaimer: I am the author.
https://github.com/web-atoms/xamarin-v8
Add NuGet Package
<PackageReference Include="Xamarin.Android.V8" Version="1.4.79" />
Code
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With