在Xamarin中从JavaScript调用C#
试图进行测试,但发现它不适用于我,我使用API19,我的代码是:在Xamarin中从JavaScript调用C#,c#,android,xamarin,C#,Android,Xamarin,试图进行测试,但发现它不适用于我,我使用API19,我的代码是: using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; using Android.Webkit; using Java.Interop; namespace App3 { [Activity(Label
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Webkit;
using Java.Interop;
namespace App3
{
[Activity(Label = "App3", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
const string html = @"
<html>
<body>
<p>Demo calling C# from JavaScript</p>
<button type=""button"" onClick=""CSharp.ShowToast()"">Call C# </button>
</body>
</html>";
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
WebView localWebView = FindViewById<WebView>(Resource.Id.LocalWebView);
localWebView.SetWebViewClient(new WebViewClient()); // stops request going to Web Browser
localWebView.Settings.JavaScriptEnabled = true;
// localWebView.LoadUrl("http://developer.xamarin.com");
// localWebView.LoadUrl("file:///android_asset/index.html");
localWebView.LoadData(html, "text/html", null);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
}
}
class MyJSInterface : Java.Lang.Object
{
Context context;
public MyJSInterface(Context context)
{
this.context = context;
}
[Export]
[JavascriptInterface]
public void ShowToast()
{
Toast.MakeText(context, "Hello from C#", ToastLength.Short).Show();
}
}
}
使用系统;
使用Android.App;
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
使用Android.Webkit;
使用Java.Interop;
名称空间App3
{
[活动(Label=“App3”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
整数计数=1;
常量字符串html=@”
从JavaScript调用C#的演示
打电话给C#
";
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
//从布局资源中获取我们的按钮,
//并在其上附加一个事件
Button Button=FindViewById(Resource.Id.MyButton);
WebView localWebView=findviewbyd(Resource.Id.localWebView);
localWebView.SetWebViewClient(新WebViewClient());//停止向Web浏览器发送请求
localWebView.Settings.JavaScriptEnabled=true;
//localWebView.LoadUrl(“http://developer.xamarin.com");
//localWebView.LoadUrl(“file:///android_asset/index.html");
LoadData(html,“text/html”,null);
button.Click+=delegate{button.Text=string.Format(“{0}clicks!”,count++);};
}
}
类MyJSInterface:Java.Lang.Object
{
语境;
公共MyJSInterface(上下文)
{
this.context=上下文;
}
[出口]
[JavascriptInterface]
公众假期
{
Toast.MakeText(上下文“Hello from C#”,ToastLength.Short).Show();
}
}
}
我犯了什么错
注意:我已经添加了对Mono.Android.Export的引用(因此您可以使用[Export]注释):在加载HTML之前,您需要将
MyJavascriptInterface
的实例添加到localWebView
:
WebView localWebView = FindViewById<WebView>(Resource.Id.LocalWebView);
localWebView.SetWebViewClient(new WebViewClient()); // stops request going to Web Browser
localWebView.Settings.JavaScriptEnabled = true;
// Add an instance of the Javascript interface named "CSharp"
localWebView.AddJavascriptInterface(new MyJSInterface(this), "CSharp");
localWebView.LoadData(html, "text/html", null);
WebView localWebView=findviewbyd(Resource.Id.localWebView);
localWebView.SetWebViewClient(新WebViewClient());//停止向Web浏览器发送请求
localWebView.Settings.JavaScriptEnabled=true;
//添加名为“CSharp”的Javascript接口实例
AddJavascriptInterface(新的MyJSInterface(this),“CSharp”);
LoadData(html,“text/html”,null);
您有我编辑答案的确切代码吗?谢谢@mattherwrdev,看起来我犯了两个错误,第一个是您修复的错误,另一个是在WevView界面上方添加了本机按钮,看起来它们本机和webView不能一起工作。