简体   繁体   English

捕获JavaScript错误WebView Xamarin表单IOS

[英]Catch JavaScript errors WebView Xamarin forms IOS

I have a WebView control in my Xamarin form that points to a URL. 我在Xamarin表单中有一个WebView控件,它指向一个URL。 I am looking for a way to see any unhandled JavaScript errors (things like we see in console of desktop browser) as part of response. 我正在寻找一种方法来查看任何未处理的JavaScript错误(例如我们在桌面浏览器控制台中看到的内容)作为响应的一部分。 please advise? 请指教?

Have a try with Invoking JavaScript in Forms as follow : 尝试使用“在表单中调用JavaScript ”,如下所示:

WebView includes the ability to invoke a JavaScript function from C#, and return any result to the calling C# code. WebView包括从C#调用JavaScript函数并将任何结果返回到调用C#代码的功能。 This is accomplished with the WebView.EvaluateJavaScriptAsync method, which is shown in the following example from the WebView sample: 这是通过WebView.EvaluateJavaScriptAsync方法完成的,该示例在WebView示例的以下示例中显示:

var numberEntry = new Entry { Text = "5" };
var resultLabel = new Label();
var webView = new WebView();
...

int number = int.Parse(numberEntry.Text);
string result = await webView.EvaluateJavaScriptAsync($"factorial({number})");
resultLabel.Text = $"Factorial of {number} is {result}.";

The WebView.EvaluateJavaScriptAsync method evaluates the JavaScript that's specified as the argument, and returns any result as a string. WebView.EvaluateJavaScriptAsync方法评估指定为参数的JavaScript,并以字符串形式返回任何结果。 In this example, the factorial JavaScript function is invoked, which returns the factorial of number as a result. 在此示例中,调用了阶乘JavaScript函数,该函数返回数字的阶乘。 This JavaScript function is defined in the local HTML file that the WebView loads, and is shown in the following example: 此JavaScript函数在WebView加载的本地HTML文件中定义,并在以下示例中显示:

<html>
<body>
<script type="text/javascript">
function factorial(num) {
        if (num === 0 || num === 1)
            return 1;
        for (var i = num - 1; i >= 1; i--) {
            num *= i;
        }
        return num;
}
</script>
</body>
</html>

Then if want to get errors from JavaScript , also can use this way . 然后如果要从JavaScript中获取错误,也可以使用这种方式。

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

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