我正在尝试在 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、
UnauthorizedAccessException
和 TargetInitationException
)不是由 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/