locked
WebView.EvaluateJavaScriptAsync always null RRS feed

  • Question

  • User77877 posted

    I'm trying to use WebView.EvaluateJavaScriptAsync.

    I'm just using this page to test with

    If you try it in Chrome and in the debug console type myFunction(123, 456)

    It says "56088".

    But in Xamarin.Forms if I do:

    string result = await webView.EvaluateJavaScriptAsync($"myFunction(123, 456)");

    result is always null

    I can do other things, and they work fine:

    string result2 = await webView.EvaluateJavaScriptAsync("27 + 92"); string result3 = await webView.EvaluateJavaScriptAsync("alert('hey')"); string documentBodyInnerHTML = await webView.EvaluateJavaScriptAsync("document.body.innerHTML");

    Anyone know how to get this working?

    Friday, May 15, 2020 7:41 PM

All replies

  • User42253 posted

    I'm struggling with the same problem (at least on UWP, didn't test on the other platforms). Did your find the issue?

    Wednesday, July 8, 2020 5:19 AM
  • User77877 posted

    No, we came up with a workaround. Luckily the front end team that controls the website was very accommodating.

    Using this as a model: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview

    We simply had the website call invokeCSCode() anytime it needed to send a value back to Xamarin. It makes everything event-based, but it works.

    So now you call:

    webView.EvaluateJavaScriptAsync($"myFunction(123, 456)");
    

    And then you get the value back:

    private void HybridWebViewActionWasInvoked(string data)
    {
        // data will be 579 here (assuming myFunction is adding the two values...)
    }
    
    Wednesday, July 8, 2020 3:11 PM