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();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…