【问题标题】:WebBrowser.InvokeScript() Throws Error: 80020006WebBrowser.InvokeScript() 引发错误:80020006
【发布时间】:2014-02-20 22:09:42
【问题描述】:

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

我正在使用 WebBrowser Control 并尝试加载具有 javascript 的 html 页面。

在 button_Click 事件中我调用 Webbrowser.InvokeScript("JavascriptFunctionName"),但它总是抛出错误:80020006,即使 WebBrowser.isScriptEnabled = true。

下面是代码

Html 页面: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 文件: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)
   {
   }

}

帮帮我.. 谢谢。

【问题讨论】:

标签: c# javascript windows-phone-8 webbrowser-control


【解决方案1】:

当您在 WP8 下使用 NavigateToString 时,脚本不会被执行。下面的测试证明了这一点,body 背景颜色不会变红。

<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>

一种可能的解决方案是在隔离存储中创建一个临时文件,如described here。

另一种选择是导航到带有Navigate("about:blank") 的空白页面,处理WebBrowser.LoadCompleted 事件,然后注入所需的HTML 和脚本,如here 所述。

【讨论】:

  • 感谢他的回复,但它不起作用..同样的错误
  • 背景颜色没有变化。我也尝试过使用 WebBrowser.Navigate(),它也不起作用。
  • 这意味着,你的&lt;script&gt;标签没有被执行。先解决这个问题,然后再回到WebBrowser.InvokeScript。我建议您将其作为一个单独的问题发布。使用正确的标签——注意我是如何编辑你的问题标签的。
猜你喜欢
  • 1970-01-01
  • 2016-01-26
  • 2020-07-12
  • 1970-01-01
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
相关资源
最近更新 更多