c# - WP7 InvokeScript 错误

标签 c# javascript windows-phone-7

我在 wp7 上使用 InvokeScript 方法时遇到一些困难:

webBrowser1.InvokeScript("eval", string.Format("document.getElementsByName('email').value='{0}'", _email));
webBrowser1.InvokeScript("eval", string.Format("document.getElementsByName('pass').value='{0}'", _pass));
webBrowser1.InvokeScript("eval", "document.forms[0].submit();");

不幸的是,当我尝试使用 (document.forms[0].submit()) 提交信息时,会抛出异常并显示以下消息:

An unknown error has occurred. Error: 80020101.

问题可能是什么?

最佳答案

首先确保 IsScriptingEnabled 为 true,但我假设您已经做到了。

您的问题可能是您太早调用了代码。当 Navigated 事件发生时,DOM 似乎还没有准备好进行操作。示例:

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();

        Wb.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(Wb_Navigated);
        MouseLeftButtonDown += new MouseButtonEventHandler(MainPage_MouseLeftButtonDown);

        Wb.NavigateToString("<html><body><form action='http://google.com/'></form></body></html>");
    }

    void Wb_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        Wb.InvokeScript("eval", "document.forms[0].submit();"); // Throws 80020101
    }

    private void MainPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Wb.InvokeScript("eval", "document.forms[0].submit();"); // Works
    }
}

关于c# - WP7 InvokeScript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9911985/

相关文章:

c# - 基于索引对二进制文件进行排序

javascript - 如何使 webpack 热模块替换与 Handlebars 模板一起使用?

c# - 为什么 Web API 不反序列化这个但 JSON.Net 会反序列化?

javascript - 在 Textarea 中的 Keyup 事件上触发 Twitter Bootstrap Popover?

javascript - 使用 AngularJS 使输入出现

C# - 在 Windows 7 Phone 中将 byte[] 转换为 String

c# - 如何使用数据绑定(bind)?

Silverlight 单元测试框架在外部类库中运行测试

c# - 错误 2 使用未分配的局部变量 'Y'

c# - 解释一下这个语句 += () => 的用途以及它是如何工作的