在 Xamarin 中调用 JavaScript 函数 [英] Call JavaScript function in Xamarin
问题描述
如何在 Xamarin 中调用 JavaScript 方法?.我找到了这个库(
再次更新:
如果你想从C#调用html JavaScript函数,你可以在MainActivity.cs中配置webview:
WebSettings settings = webview1.Settings;settings.JavaScriptEnabled = true;//加载javascript接口方法调用前台方法//webview1.AddJavascriptInterface(new MyJSInterface(this), "CSharp");webview1.SetWebViewClient(new WebViewClient());webview1.LoadUrl("file:///android_asset/login.html");
请在Assets文件夹中添加html文件,并将BuildAction设置为AndroidAsset.
然后C#调用JS中的一个方法并获取回调值
class EvaluateBack : Java.Lang.Object, IValueCallback{public void OnReceiveValue(对象值){Toast.MakeText(Android.App.Application.Context,value.ToString(), ToastLength.Short).Show();//你会得到值100"}}webview1.EvaluateJavascript("javascript: check();", new EvaluateBack());
这是github上的示例,您可以看看:
https://github.com/CherryBu/CallJavaFunction
How can i call JavaScript method in Xamarin?. I found library to this(https://github.com/chkn/HybridKit), but unfortunately, this tool can call C# function in JavaScript. For Xamarin.Forms i found one solution https://docs.microsoft.com/en-au/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview
You could want to Call html JavaScript function in C#, if yes, you can take a look:
There are index.html,added in PCL,setting Build Action as EmbeeddedResource, there is one function, named printMultiplicationTable
<html>
<body>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<div id='multiplicationtable'></div>
<script type="text/javascript">
function printMultiplicationTable(num, stop)
{
var number = parseInt(num);
var stopNumber = parseInt(stop);
$('#multiplicationtable').empty();
for (var index = 1; index <= stopNumber; index++) {
$('#multiplicationtable').append(number + ' x ' + index + " = " + number * index + '<br/>');
}
}</script>
</body>
</html>
Now, you want to call this JavaScript function in C#,
<StackLayout>
<Label
FontSize="Medium"
HorizontalOptions="Center"
Text="Multiplication Table" />
<StackLayout HorizontalOptions="Center" Orientation="Horizontal">
<Label Text="Number: " VerticalOptions="Center" />
<Entry
x:Name="numberEntry"
Text="5"
WidthRequest="40" />
<Label Text="Stop:" VerticalOptions="Center" />
<Entry
x:Name="stopEntry"
Text="10"
WidthRequest="40" />
</StackLayout>
<Button
x:Name="callJavaScriptButton"
Clicked="OnCallJavaScriptButtonClicked"
Text="Call JavaScript" />
<WebView
x:Name="webView"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand" />
</StackLayout>
public MainPage()
{
InitializeComponent();
webView.Source = LoadHTMLFileFromResource();
}
HtmlWebViewSource LoadHTMLFileFromResource()
{
var source = new HtmlWebViewSource();
// Load the HTML file embedded as a resource in the PCL
var assembly = typeof(MainPage).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("CallJavaScript.index.html");
using (var reader = new StreamReader(stream))
{
source.Html = reader.ReadToEnd();
}
return source;
}
private void OnCallJavaScriptButtonClicked(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(numberEntry.Text) || string.IsNullOrWhiteSpace(stopEntry.Text))
{
return;
}
int number = int.Parse(numberEntry.Text);
int end = int.Parse(stopEntry.Text);
webView.Eval(string.Format("printMultiplicationTable({0}, {1})", number, end));
}
This is my sample, you can get it from github:
https://github.com/CherryBu/CallJavaScript
Update:
Update again:
If you want to call html JavaScript function from C#, you can configure webview in MainActivity.cs:
WebSettings settings = webview1.Settings;
settings.JavaScriptEnabled = true;
// load the javascript interface method to call the foreground method
//webview1.AddJavascriptInterface(new MyJSInterface(this), "CSharp");
webview1.SetWebViewClient(new WebViewClient());
webview1.LoadUrl("file:///android_asset/login.html");
Please add html file in Assets folder, and set BuildAction as AndroidAsset.
Then C# calls a method in JS and gets the callback value
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"
}
}
webview1.EvaluateJavascript("javascript: check();", new EvaluateBack());
This is sample at github you can take a look:
https://github.com/CherryBu/CallJavaFunction
这篇关于在 Xamarin 中调用 JavaScript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!