c# - 从 C# 调用 JavaScript 时出现错误 UnathorizedException

标签 c# javascript cordova windows-phone-8 invokescript

我正在尝试在 PhoneGap (Cordova) Windows Phone 8 应用程序中从 C# 调用 JavaScript 代码。按照此接受的答案线程的步骤:How to call JavaScript from C# - Cordova/PhoneGap

我应该能够访问 Javascript 部分中的方法或执行硬编码的 JS 代码。但是,我遇到了这个错误:

An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
An exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll but was not handled in user code
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll
An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
ERROR: Exception in ProcessCommand :: Exception has been thrown by the target of an invocation.
ERROR: failed to InvokeMethodNamed :: pluginMethod on Object :: Example

我的代码是(是 native 插件):

namespace WPCordovaClassLib.Cordova.Commands
{
    public class Example : Cordova.Commands.BaseCommand
    {
        public void pluginMethod(string options)
        {
          //The error is caused in this line
          PGWebBrowserHandler.getInstance().webView.CordovaBrowser
              .InvokeScript("eval", 
                            new string[] { "alert('Is it running?!');" });
        }
    }
}

PGWebBrowserHandler 是前面提到的线程中给出的单例类的名称,以便从任何 C# 类访问 CordovaBrowser

错误(FileNotFoundException、 UnauthorizedAccessExceptionTargetInitationException)不是由 InvokeScript() 方法引起的,之前是通过获取webView引起的,它不会崩溃,但是调试它时,很多is方法都会导致UnauthorizedAccessException。

我希望我的解释很清楚。我一直在谷歌搜索它,但我没有在任何地方找到解决方案或解决方法。 有人知道这些问题的原因吗?从 MainPage.cs 调用 invokeScript() 方法也会导致异常。

最佳答案

试试这个代码:

Dispatcher.BeginInvoke(
            () =>
            {
                PGWebBrowserHandler.getInstance().webView.CordovaBrowser
                    .InvokeScript("eval",
                      new string[] { "alert('Is it running?!');" });
            });

关于c# - 从 C# 调用 JavaScript 时出现错误 UnathorizedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22540159/

相关文章:

JavaScript 安全 : force deletion of sensitive data

javascript - Phonegap + JQuery 无法连接到互联网

c# - 如何使用 C# 从 ASPX 页面中获取 IIS 中的网站列表?

c# - 为一个参数指定多个接口(interface)

javascript - 自动弹出框显示时禁用背景

javascript - Jquery 上传 - MVC 文件名不起作用

javascript - 如何将 Angular UI-Grid 过滤后的完整数据导出到 Excel 中

ios - 如何在 IOS cordova 项目上自动设置 Signing Team?

c# - 寻找可能的组合 linq

c# - EntityFramework Linq Left Join 解析错误 - DotNet Core 1.0