简体   繁体   中英

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

I have this Xamarin Android project in which I need to call a Javascript method from C#. The name of the method is "drawMap". I have used the following technique to call the method.

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

But this MainActivity class is at the root of the project and Javascript class is inside Views-> test.js. How do I set the path to look for this method in that class?

Below code exists in the RazorView.cshtml file.

 $(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;
        }
 }

This drawMap is the method I have to call from C#

You can call JS in WebView by LoadUrl method, first be sure you set Enable to true for Settings.JavaScriptEnabled .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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