Xamarin Android Javascript 和 C# 之间的两种通信方式 [英] Xamarin Android two way communication between Javascript and C#
问题描述
我正在使用 Xamarin Android 开发一个应用程序,它有一个显示网页的 WebView.我想实现从 WebView 到 C# 的 Javascript 之间的双向通信.我可以使用这个 link 从 Javascript 调用 C#.但是我找不到将数据从 C# 发送回 Javascript 的方法.有没有办法以这种方法来回发送数据.我认为用 Javascript 编写回调会起作用,但如何从 C# 代码中触发它.
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.
现在,我的问题是如何从 javascript 接口类调用 WebView.我有一个 Javascript 接口类 https://developer.xamarin.com/食谱/android/controls/webview/call_csharp_from_javascript/命名空间扫描器Android{公共类 JSInterface:Java.Lang.Object{上下文上下文;WebView webView;
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');");
}
}
}
代码在 LoadUrl 行抛出异常.java.lang.Throwable:在线程Thread-891"上调用了 WebView 方法.所有 WebView 方法都必须在同一线程上调用.(预期的 Looper Looper (main, tid 1) {42ce58a0} 在 null 上调用,仅供参考,主要 Looper 是 Looper (main, tid 1) {42ce58a0})
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})
现在我正在努力如何从这个 Java 脚本接口类中引用 WebView
Now i am struggling how to refer the WebView from this Java script interface class
推荐答案
是的.那是可能的.如果您的目标是 KitKat 或更高版本,则可以使用:
Yes. That is possible. If you are targeting KitKat or higher you can use:
webView.EvaluateJavascript("enable();", null);
在这种情况下 enable();
是一个 JS 函数.
Where in this case enable();
is a JS function.
如果您的目标是较低的 API 级别,您可以使用 LoadUrl();
:
If you are targeting lower API levels you can use LoadUrl();
:
webView.LoadUrl("javascript:enable();");
您在 LoadUrl
上抱怨的错误是因为它出于某种原因发生在非 UI 线程上.
The error you get where it complains on LoadUrl
is because it for some reason happens on a non-UI thread.
既然您已经将 Context
传递到您的 JavascriptInterface 类中,那么您可以简单地将 ShowToast
的内容包装在:
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
});
只需将签名从 Context
更改为 Activity
,它应该可以帮助您将您编组回 UI 线程.
Just change signature from Context
to Activity
and it should help you marshal you back on UI thread.
这篇关于Xamarin Android Javascript 和 C# 之间的两种通信方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!