locked
Using a javascript library in a Net Standard Library project from Xamarin forms? RRS feed

  • Question

  • User372105 posted

    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

    Thursday, December 5, 2019 8:12 PM

Answers

  • User372105 posted

    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

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, December 12, 2019 12:58 PM

All replies

  • User382871 posted

    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. public class JavascriptWebViewClient : WebViewClient { string _javascript; public JavascriptWebViewClient(string javascript) { _javascript = javascript; } public override void OnPageFinished(WebView view, string url) { base.OnPageFinished(view, url); view.EvaluateJavascript(_javascript, null); } }

    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/

    Friday, December 6, 2019 8:58 AM
  • User372105 posted

    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.js

    I 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).

    Monday, December 9, 2019 2:15 PM
  • User382871 posted

    @Lelo said: 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.js

    I 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.

    Tuesday, December 10, 2019 2:14 AM
  • User372105 posted

    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

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, December 12, 2019 12:58 PM