1

I'm working on a project in Xamarin C# and I'm having trouble running some Java Script. Is there currently a way to call a specific JS function and receive its return value?

I know that JS can be run in a WebView; however, how can I get the output?

At the moment, the JS is coming from a link on my site. Any help would be very much appreciated!


EDIT

Here's the code I've tried:

MyView.axml

 <android.webkit.WebView
            android:id="@+id/web_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            local:layout_constraintTop_toBottomOf="@+id/pause"
            local:layout_constraintBottom_toBottomOf="parent"/>

MyView.cs

called in protected override void OnCreate

webView.Settings.JavaScriptEnabled = true;
webView.SetWebViewClient(new MyWebViewClient());
webView.LoadUrl("http://www.example.com/scheduleV5.js");

MyWebViewClient (class)

public class MyWebViewClient : WebViewClient
{
    public override void OnPageFinished(WebView view, String url)
    {
        view.EvaluateJavascript("javascript: getSchedule();", new EvaluateBack());
    }
}

EvaluateBack (class)

public class EvaluateBack : Java.Lang.Object, IValueCallback
{

    public void OnReceiveValue(Java.Lang.Object value)
    {
        Toast.MakeText(Android.App.Application.Context, value.ToString(), ToastLength.Short).Show();// you will get the value "100"
        var test = value.ToString();
    }
}
2

1 Answer 1

3

you could do like this

Js calls methods in C#

Configure the OnCreate method in the Activity :

var webview = FindViewById<WebView>(Resource.Id.webView1);
WebSettings settings = webview.Settings;
settings.JavaScriptEnabled = true;
// load the javascript interface method to call the foreground method
webView.AddJavascriptInterface(new MyJSInterface(this), "CSharp");
webview.SetWebViewClient(new WebViewClient());

Create a C# class :

class MyJSInterface : Java.Lang.Object
  {
    Context context;

  public MyJSInterface (Context context)
    {
      this.context = context;
    }

  [JavascriptInterface]
  [Export]
  public void ShowToast (string msg)
    {
      Toast.MakeText(context, msg, ToastLength.Short).Show();
    }
  }

And then it's called in JS :

<button type="button" onClick="CSharp.ShowToast ('Call C#')">Call C#</button>

You can refer to this document:https://github.com/xamarin/recipes/tree/master/Recipes/android/controls/webview/call_csharp_from_javascript

C# calls a method in JS and gets the callback value

webView.EvaluateJavascript("javascript: callJS();", new EvaluateBack());

class EvaluateBack : Java.Lang.Object,IValueCallback
    {

        public void OnReceiveValue(Object value)
        {

            Toast.MakeText(Android.App.Application.Context, value.ToString(), ToastLength.Short).Show();// you will get the value "100"
        }
    }

in JS :

<script>
      function callJS(){
        return 100;
      }
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

could it help you ?
I might be a little confused, do you want to call the JS method in C# and get the callback value? If so, please refer to the second method I updated
Perfect! the second method is what I'm looking for. I'll try it out and let you know.
I keep getting a null return value. Just to clarify I call: webView.LoadUrl("http://mywebsite.com/file.js"); then evaluatescript?
You could customize a "WebViewClient" and then call "webview.evaluatescript" in "OnPageFinished"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.