繁体   English   中英

使用Xamarin android从C#调用Javascript方法时,如何指定Javascript路径?

[英]How to specify Javascript path when calling Javascript method from C# using Xamarin android?

我有一个Xamarin Android项目,需要从C#调用Javascript方法。 该方法的名称是“ drawMap”。 我使用以下技术来调用该方法。

webView.EvaluateJavascript(string.Format("mapDraw(" + ConvertToRadians(degree) + ")"), null);

但是此MainActivity类位于项目的根目录,而Javascript类位于Views-> test.js内部。 如何设置在该类中查找此方法的路径?

RazorView.cshtml文件中存在以下代码。

 $(document).ready(function () {
     function mapDraw(radValue) {
            var updatedPoint = mapImg.getMapCoordinates(currentPoint[0] + Math.sin(radValue), currentPoint[1] - Math.cos(radValue), false);
            console.log(currentPoint);
            console.log(updatedPoint);
            var line = new fabric.Line([currentPoint[0], currentPoint[1], currentPoint[0], currentPoint[1]], {
                stroke: 'red',
                originX: 'center',
                originY: 'center'
            });

            canvas.add(line);
            line.set({
                x2: updatedPoint[0],
                y2: updatedPoint[1]
            });
            canvas.renderAll();
            currentPoint = updatedPoint;
        }
 }

这个drawMap是我必须从C#调用的方法

您可以通过LoadUrl方法在WebView中调用JS,首先请确保将Settings.JavaScriptEnabled Enable设置为true。

WebView wv = FindViewById<WebView>(Resource.Id.webView1);
wv.Settings.JavaScriptEnabled = true;
wv.Settings.AllowFileAccess = true;

webview.LoadUrl("javascript:mapDraw(\"" + ConvertToRadians(degree) + "\")");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM