WebBrowser控件中的“预期功能”

时间:2018-07-05 14:43:28

标签: c# .net internet-explorer webbrowser-control

我在WebBrowser控件中显示以下HTML代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Welcome</title>
  <style type="text/css">
    /*<![CDATA[*/
      body {
        font: 8pt Verdana;
        background: ThreeDFace;
      }
      #appname {
        line-height: 1;
      }
      #appname h1 {
        margin: 0;
        font: bold 10pt Verdana;
      }
      #appname p {
        margin: 0;
      }
      #actions_list {
        list-style-type: none;
        padding: 0;
      }
    /*]]>*/
  </style>
  <script type="text/javascript">
    //<![CDATA[
      var ACTION_NEWPROJECT  = 0x0001;
      var ACTION_NEWFILE     = 0x0002;
      var ACTION_OPENPROJECT = 0x0003;
      var ACTION_OPENFILE    = 0x0004;
    //]]>
  </script>
</head>
<body>
  <div id="appname">
    <h1>Application</h1>
    <p>Slogan</p>
  </div>
  <div id="actions">
    <ul id="actions_list">
      <li><a href="#" onclick="window.external.ExecuteAction(ACTION_NEWPROJECT);return false">New project</a></li>
      <li><a href="#" onclick="window.external.ExecuteAction(ACTION_NEWFILE);return false">New file</a></li>
      <li><a href="#" onclick="window.external.ExecuteAction(ACTION_OPENPROJECT);return false">Open project</a></li>
      <li><a href="#" onclick="window.external.ExecuteAction(ACTION_OPENFILE);return false">Open file</a></li>
    </ul>
  </div>
  <div id="recentfiles">
    <p>Recent files:</p>
    <ul id="recentfiles_list"></ul>
  </div>
  <script type="text/javascript">
    //<![CDATA[
      window.onload = function() {
        var recentFiles = window.external.GetRecentFiles();
        var recentFilesDOMContainer = document.getElementById("recentfiles_list");
        for (var i = 0; i < recentFiles.length; i++) {
          var index = 0+i;
          var fileDOMParent = window.document.createElement("li");
          var fileDOMElement = window.document.createElement("a");
          fileDOMElement.href = "#";
          fileDOMElement.onclick = function() {
            window.external.OpenRecentFile(index);
            return false;
          };
          fileDOMElement.innerText = recentFiles[i];
          fileDOMParent.appendChild(fileDOMElement);
          recentFilesDOMContainer.appendChild(fileDOMParent);
        }
      };
    //]]>
  </script>
</body>
</html>

要与应用程序通信,我正在使用WebBrowser.ObjectForScripting属性,因此我可以通过window.external访问具有函数的类。效果也很好。

但是当我尝试打开下面的页面时,出现以下错误:

Script Error
Line: 61
Char: 25
Error: Function expected
Code 0

为什么会这样?它似乎发生在for循环中。

1 个答案:

答案 0 :(得分:0)

我唯一能想到的是,recentFiles中没有数组。您是否尝试打开开发人员控制台并输入“ recentFiles”以查看其内容? :)