WebBrowser.InvokeScript()抛出错误:80020006 [英] WebBrowser.InvokeScript() Throws Error: 80020006

查看:761
本文介绍了WebBrowser.InvokeScript()抛出错误:80020006的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款Windows Phone 8应用程序,我是新来的Windows Phone 8。

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

我使用WebBrowser控件,并尝试加载具有HTML页面的JavaScript。

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

在button_Click事件我打电话Webbrowser.InvokeScript(JavascriptFunctionName),但它总是抛出错误:80020006,即使WebBrowser.isScriptEnabled = TRUE

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

下面是代码

HTML页面:MyHtmlPage.html

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>



铯文件:MainPage.cs

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.

推荐答案

脚本当您使用 NavigateToString WP8下都没有得到执行。 。下面的测试证明了这一点,身体的背景颜色不会变成红色。

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>



一个可能的解决方案是在独立存储创建一个临时文件,的这里描述

另一个选择是导航到一个空白页导航(关于:空白),处理的 WebBrowser.LoadCompleted 的事件,然后注入所需的HTML和脚本,如上所述这里

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.

这篇关于WebBrowser.InvokeScript()抛出错误:80020006的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆