What is the easiest way to call c# method from javascript in webview?

I have a webview, there is a javascript inside, how can I call/trigger C# method from it? There is an article https://developer.xamarin.com/recipes/android/controls/webview/call_csharp_from_javascript/ in documentation, but that applies only for android and I need it on Android and iOS.

Best Answers


  • cuddlecheekcuddlecheek CZMember ✭✭

    I mean call C# from javascript, not call javascript from C#

  • RyanDixonRyanDixon USMember ✭✭✭
    edited August 2016

    Its a bit of a messy workaround, but XLabs provides a "HybridWebView" with the functionality you are looking for.

    Start by binding a method to your webview using the following code:
    MyWebView.RegisterCallback("nameOfFunctionInJavascript", csCallbackAction);

    Then call your code in Javascript using the "Native" wrapper
    Native("nameOfFunctionInJavascript", "arg0", "arg1")

    Hope this helps,

  • cuddlecheekcuddlecheek CZMember ✭✭

    @RyanDixon So it is not possible using standart Xamarin Forms WebView?

  • AdrianoPetrucciAdrianoPetrucci USMember ✭✭

    @Hunuman said:

    It is fairly simple to invoke predefined C# methods from JavaScript inside a WebView:

    1. In the JS set up and navigate to a proxy URL that indicates the name of the method to be called and any parameters in the query string. eg. http://CSCommand.htm?cmdName=Method1&custid=2778&amout=27.50

    2. In the WebView's Navigating event interrogate the URL and its querystring to see if a method call has been requested.
      If so Cancel the navigation and then extract the name and parameters from the URL and call the method.

    Hope this helps,


    Thank you Tim! This seem the best solution for many projects.
    For example to start the QR code scanner:

    So it is also possible to set some parameters or configure the application through this command.

  • JohnHardmanJohnHardman GBUniversity mod
    edited June 2017

    @Hunuman - Nice solution. Just wished I'd tried it before trying various alternatives, none of which worked on iOS. Just wondering whether to remove my alternative solutions for Android and UWP now ;-) Thanks for the guidance - good, reliable method.

  • Nani_777Nani_777 INMember ✭✭

    @Hunuman @AdrianoPetrucci I dont understand how to do this. Should I just specify my function name as http://functionname

  • @Nani_777 Exactly.
    (I am not allowed to insert http on the posts... So replace all XXXXXXX with http - thank you Xamarin!)
    There are some ways to do it. The WebView can catch URL-calls. So you can simply catch an URL and if this has the function name, you can call your JavaScript.

    Note: If many uses this "XXXXXXX://functionname" it is a wrong formatted link and is not accepted as http protocol. Android will ignore it, Windows will cause an exception. "http:" is the right way to write. If you write "XXXXXXX:/" it means that you have an archive system. At this point you have to put at least a folder on it if you want to be conform. So use something like "XXXXXXX:/APP_NAME/function" or "XXXXXXX:/FUNCTION/?Params".

    If you define your own protocol, you can add it to your app. So if browsing on a webpage, the phone can open your app if someone open that link (example: "APP_NAME:/FUNCTION").

    You can check my project if you want on github:
    github. com/Adrianotiger/qr2web

Sign In or Register to comment.