How do I call Javascript from C# in a UWP Renderer?

CaptainXamtasticCaptainXamtastic GBUniversity ✭✭✭

How do I call a Javascript function in a UWP renderer that uses a Windows.UI.Xaml.Controls.WebView?

It has to be done in the UWP Renderer, using the Windows.UI.Xaml.Controls.WebView object, not in the Forms project using the Xamarin.Forms.WebView.

Any ideas?

Posts

  • JohnHardmanJohnHardman GBUniversity mod

    @CaptainXamtastic said:
    How do I call a Javascript function in a UWP renderer that uses a Windows.UI.Xaml.Controls.WebView?

    It has to be done in the UWP Renderer, using the Windows.UI.Xaml.Controls.WebView object, not in the Forms project using the Xamarin.Forms.WebView.

    Any ideas?

    This is how the Xamarin.Forms.Platform.UWP.WebViewRenderer does it:

            async Task<string> OnEvaluateJavaScriptRequested(string script)
            {
                return await Control.InvokeScriptAsync("eval", new[] { script });
            }
    
  • CaptainXamtasticCaptainXamtastic GBUniversity ✭✭✭
    edited February 16

    Thanks @JohnHardman, how do I use it?

    The existing Javascript function name that I want to call is Terminate() and nothing is passed to it, so do I go:

    var return = await Control.InvokeScriptAsync("eval", new[] { 'Terminate()' });
    

    (I'm not understanding the eval)

    The method's signature in the API https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.webview.invokescriptasync#Windows_UI_Xaml_Controls_WebView_InvokeScriptAsync_System_String_Windows_Foundation_Collections_IIterable_System_String__ is:

    public IAsyncOperation<string> InvokeScriptAsync(String scriptName, IEnumerable<String> arguments)
    

    So I thought it would be:

    var return = await Control.InvokeScriptAsync("Terminate()", new[] { });
    

    Sorry to ask when I could simply do the calls but our team is set up oddly and I'm not in the position to test the success of the Javascript call, the first I will hear of it's failure is when it fails QA :cold_sweat:

    So which of the above would you go for (even if it's just a 'cross-the-fingers' suck and see).

    Thanks!

  • JohnHardmanJohnHardman GBUniversity mod

    @CaptainXamtastic said:
    Thanks @JohnHardman, how do I use it?

    The existing Javascript function name that I want to call is Terminate() and nothing is passed to it, so do I go:

    var return = await Control.InvokeScriptAsync("eval", new[] { 'Terminate()' });
    

    It's a long time since I last investigated InvokeScriptAsync. I use it to inject a load of JavaScript into HTML pages, then call those methods and retrieve results. Given how much time I spent on it, it's scary that I cannot remember 100%. Off the top of my head, I'd say that the above looks right, although as a matter of preference I'd use double quotes around the Terminate() rather than single quotes.

    @CaptainXamtastic said:
    Sorry to ask when I could simply do the calls but our team is set up oddly and I'm not in the position to test the success of the Javascript call, the first I will hear of it's failure is when it fails QA :cold_sweat:

    You could load up any web page into the WebView and call a JavaScript method. It doesn't have to be Terminate().

  • CaptainXamtasticCaptainXamtastic GBUniversity ✭✭✭

    @JohnHardman Thanks very much indeed! I'll look at this on Tuesday (I'm on holiday until then!).

    Cheers!

  • CaptainXamtasticCaptainXamtastic GBUniversity ✭✭✭

    @JohnHardman Thanks - and guess what, both of the following work:

    var myresult = await Control.InvokeScriptAsync("Terminate", new[] { "" });
    

    (the above is in line with the api documentation)

    and

    var myresult = await Control.InvokeScriptAsync("eval", new[] { "Terminate()" });
    

    Thanks again!

    Btw, testing was easy, I overlooked the fact that the value of myresult would tell me what had been returned by the Terminate function (a 1 or 0) (and whether that constituted success of failure).

  • JohnHardmanJohnHardman GBUniversity mod

    @CaptainXamtastic - Please remember to mark as Accepted Answer and/or Like any posts that helped.

Sign In or Register to comment.