WebBrowser.InvokeScript() Throws Error: 80020006

蹲街弑〆低调 提交于 2019-12-11 02:56:34

问题


I am developing a Windows Phone 8 App and i am new to Windows Phone 8.

I am using WebBrowser Control and try to load the html page having javascript.

On button_Click event i am calling Webbrowser.InvokeScript("JavascriptFunctionName"), but it always throws the error: 80020006, even though the WebBrowser.isScriptEnabled = true.

Below is the code

Html Page: MyHtmlPage.html

<!DOCTYPE html>
<html>
<head title="test">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
    html {
        height: 100%;
    }

    body {
        height: 100%;
        margin: 0;
        padding: 0;
    }

    #map-canvas {
        height: 100%;
    }
</style>

<script type="text/javascript">
    function initialize() {

        var helloText = document.getElementById("Hello");
        helloText.textContent = "Initialized";

        return ("Initialize");

    }

</script>
</head>
<body>
    <div id="test-canvas" />
    <label id="Hello">Hello</label>
</body>
</html>

Cs File : MainPage.cs

private string TestUri = @"Html/Test.html"; 
private string stringHtml = string.Empty;

public MainPage()
{
    InitializeComponent();
    WebBrowser.IsScriptEnabled = true;
    WebBrowser.IsGeolocationEnabled = true;
}

private void WebBrowser_Loaded(object sender, RoutedEventArgs e)
{            
    var resource = App.GetResourceStream(new Uri(TestUri, UriKind.Relative));
    stringHtml = new StreamReader(resource.Stream).ReadToEnd();
    WebBrowser.NavigateToString(stringHtml);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
   try
   {
       //Error 
       string returnValue = WebBrowser.InvokeScript("initialize");
   }
   catch(Exception ex)
   {
   }

}

Help me.. Thanks.


回答1:


Scripts are not getting executed when you use NavigateToString under WP8. The following test proves this, the body background color doesn't turn red.

<script type="text/javascript">
    document.body.style.backgroundColor = "red";

    window.initialize = function() {
        document.body.style.backgroundColor = "blue";

        var helloText = document.getElementById("Hello");
        helloText.textContent = "Initialized";

        return ("Initialize");
    }
</script>

One possible solution is to create a temporary file in the isolated storage, as described here.

Another option is to navigate to a blank page with Navigate("about:blank"), handle WebBrowser.LoadCompleted event, then inject the desired HTML and scripts, as described here.



来源:https://stackoverflow.com/questions/21425446/webbrowser-invokescript-throws-error-80020006

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!