Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

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

LeloLelo Member ✭✭✭

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

Best Answer

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited December 2019

    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/

  • LeloLelo Member ✭✭✭
    edited December 2019

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

  • JarvanJarvan Member, Xamarin Team Xamurai

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

  • LeloLelo Member ✭✭✭
    edited December 2019 Accepted Answer

    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

Sign In or Register to comment.