WP8.1 InvokeScript Error

2019-07-01 15:38发布

问题:

I am working on a Windows Phone 8.1 App. I have a problem with the InvokeScript method of the WebBrowser class. I have this in my XAML:

And when I run this code:

myWebBrowser.Loaded += (object sender, RoutedEventArgs e) =>
{
    webBrowser.IsScriptEnabled = true;
    webBrowser.InvokeScript("eval", "alert('foo')");
};

I get a System.SystemException on the line with InvokeScript that tells me this:

An unknown error has occurred. Error: 80020006.

When I run the same code for Windows Phone 8 (not 8.1) I don't get the exception.

Any ideas why I get an error with WP 8.1?

回答1:

I found the solution here.

The Loaded event is called too early. I used the LoadCompleted event instead. As long as I navigate to something (I've been putting webBrowser.NavigateToString(""); ) the LoadCompleted event will be called only when the WebBrowser control has fully loaded content.

Here is the new code :

webBrowser.LoadCompleted += (object sender, NavigationEventArgs e) =>
{
    webBrowser.IsScriptEnabled = true;
    webBrowser.InvokeScript("eval", "alert('foo');");
};