locked
JavaScript CallBack throwing TypeError RRS feed

  • Question

  • User376644 posted

    I am adding a webview with javascript enabled and also with the javascript interface in my Xamarin.android project. But the moment I try to call the native C# method from javascript I am getting "Uncaught TypeError: Cannot read property 'showMessage' of undefined.

    Code to enable javascript interface: Control.Settings.AllowFileAccess = true; Control.Settings.LoadWithOverviewMode = true; Control.Settings.AllowUniversalAccessFromFileURLs = true; Control.Settings.JavaScriptEnabled = true; Control.Settings.DatabaseEnabled = true; Control.Settings.DomStorageEnabled = true; Control.AddJavascriptInterface(new JavaScriptInterface(this), "TextSelection") ;

    Javascript interface code: [Export] [JavascriptInterface] public void showMessage(string message) { Console.WriteLine("Javascript_callback--> message " + message);
    }

    Code to call the C# method from javascript: var text = window.getSelection().toString(); TextSelection.showMessage(text);

    I am not getting where I am going wrong.

    Note: I tried with the below code also, but it didn't work. [JavascriptInterface] [Export("showMessage")]
    public void showMessage(string message) { Console.WriteLine("Javascript_callback--> message " + message); }

    Monday, November 25, 2019 2:13 PM

All replies

  • User371688 posted

    You can first check this following document:
    https://github.com/xamarin/docs-archive/tree/master/Recipes/android/controls/webview/callcsharpfrom_javascript

    https://stackoverflow.com/questions/9745168/monodroid-javascript-call-back/9746911

    Tuesday, November 26, 2019 10:08 AM
  • User376644 posted

    @jezh: Yes I did the same way. Then also issue exist.

    Tuesday, November 26, 2019 3:44 PM
  • User371688 posted

    Sorry,I couldn't reproduce this question, You can try to delete the bin folders and the obj folders and rebuild it ? If it doesn’t work ,try to restart the VS . If the problem persists,could you please post a basic demo so that we can test on our side?

    Wednesday, November 27, 2019 8:57 AM