I would like to use a javasscript sensor fusion library. Preferably in a Net Standard Library. Most question raised are about looking to do something ui related. What I need to do is to invoking a method in a js file - and get a return value in C#. Does anyone know a way I could achieve that?
Here are is an attempted using Jurrassic but it is not working:
https://stackoverflow.com/questions/59154143/resources-in-net-standard-library-project
I used JInt
and Jint.CommonJS
- to be able to handle js modules:
https://github.com/tylerjwatson/Jint.CommonJS
Some edits had to be made in the js library, like replacing **
with Math.pow
and foreach (with =>
) loop to for loop.` and it seems the only way where to place the js files in platform specific projects - following following how to set up resources for WebView: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=windows
Answers
To invoke a method in a js file in Xamarin.Forms, you need to create
WebView
custom renderer. Create a WebViewClient to inject the Javascript function and set the webclient to custom webview.Tutorial about Interacting with JavaScript in C # you can refer to:
https://geeks.ms/xamarinteam/2019/02/28/interact-with-javascript-from-c-using-xamarin-forms/
Thanks for the helpful snippet and link. But I think I will use a nuget: Jint instead. It allows me to run the js code any time - and without the need to render a webview. The library is limited to some extent though as -
require
,import
can't be interpreted. In my use case i'ts not needed as all I need is in one file in the library: https://github.com/psiphi75/ahrs/blob/master/Madgwick.jsI will still need to set my js fil as resources in all platform specific projects though. Unless there is some way add it once inside the net standard library project (see link to SO question in my first post).
If you have solved the issue, please mark your solution as the answer. It'll help others who face the similar problem.
I used
JInt
and
Jint.CommonJS
- to be able to handle js modules:https://github.com/tylerjwatson/Jint.CommonJS
Some edits had to be made in the js library, like replacing
**
withMath.pow
and foreach (with=>
) loop to for loop.` and it seems the only way where to place the js files in platform specific projects - following following how to set up resources for WebView: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=windows