I am developing an app using Xamarin Android which has a WebView displaying a web page. I want to implement a two way communication between Javascript from WebView to c#. I could call C# from Javascript using this link. However i couldn't find a way to send data back from C# to Javascript. Is there a way to send data back and forth in this approach. I thought writing a callback in Javascript would work but how to fire it from C# code.
Now, My problem is how to call WebView from a javascript interface class. I have a Javascript interface class as mentioned https://developer.xamarin.com/recipes/android/controls/webview/call_csharp_from_javascript/ namespace ScannerAndroid { public class JSInterface: Java.Lang.Object { Context context; WebView webView;
public JSInterface (Context context, WebView webView1)
{
this.context = context;
this.webView = webView1;
}
[Export]
[JavascriptInterface]
public void ShowToast()
{
Toast.MakeText (context, "Hello from C#", ToastLength.Short).Show ();
this.webView.LoadUrl ("javascript:callback('Hello from Android Native');");
}
}
}
The code throws an exception at LoadUrl line. java.lang.Throwable: A WebView method was called on thread 'Thread-891'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {42ce58a0} called on null, FYI main Looper is Looper (main, tid 1) {42ce58a0})
Now i am struggling how to refer the WebView from this Java script interface class
Cross-platform Xamarin is part of the vibrant . NET ecosystem, used by millions of developers worldwide. Share more than 75% of your code across platforms, for "write once, run anywhere" ease. Use your favorite frameworks, tools, and Xamarin's powerful libraries to access native APIs and 2D graphics from shared code.
A Xamarin. Forms WebView is a view that displays web and HTML content in your app. This article explains how to create a custom renderer that extends the WebView to allow C# code to be invoked from JavaScript.
Xamarin. Forms will continue to receive service releases through November 2022.
In May 2020, Microsoft announced that Xamarin. Forms, a major component of its mobile app development framework, would be deprecated in November 2021 in favour of a new . Net based product called MAUI - Multiform App User Interface.
Yes. That is possible. If you are targeting KitKat or higher you can use:
webView.EvaluateJavascript("enable();", null);
Where in this case enable();
is a JS function.
If you are targeting lower API levels you can use LoadUrl();
:
webView.LoadUrl("javascript:enable();");
EDIT:
The error you get where it complains on LoadUrl
is because it for some reason happens on a non-UI thread.
Since you have already passed on the Context
into your JavascriptInterface class, then you can simply wrap the contents of ShowToast
in:
context.RunOnUiThread(() => {
// stuff here
});
Just change signature from Context
to Activity
and it should help you marshal you back on UI thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With