2014-09-20 89 views
1

我在包含webbrowser控件的VS2012中编写了一个小应用程序。我想加载一个包含javascript的html文件。 webbrowser控件无法处理javascript默认值。所以我一直在做一些阅读。加载并调用包含javascript的html文件

我有以下的HTML文件:

<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
function initialize() { 
    var centerlatlng = new google.maps.LatLng(45.046006, -105.245867); 
    var myOptions = { 
     zoom: 13, 
     center: centerlatlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
} 
</script> 
</head> 
<body style="margin:0px; padding:0px;"> 
<div id="map_canvas" style="width: 100%; height: 100%;"></div> 
</body> 
</html> 

位于我的C#项目(VS2012)的调试目录中的HTML文件。

我想加载这个html文件并调用初始化函数。我一直在寻找关于如何做到这一点的清晰教程很长一段时间,但没有取得任何成功。随着我的知识,我尝试了以下。但是,与预期的结果不符。

代码:

maps_browser.DocumentText = File.ReadAllText("GPSmap.html"); 
maps_browser.Document.InvokeScript("initialize"); 

所以我的问题是:

  1. 如何加载包含JavaScript到网页浏览器一个* .html文件?
  2. 如何从html文件调用javascript函数?
  3. 如何正确导航到html文件?

    预先感谢任何建议:)

回答

0

它看起来像下面的代码为我工作:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     maps_webbrowser.DocumentText = File.ReadAllText(@"GPSmap.html"); 
    } 

    private void maps_webbrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 
     Debug.WriteLine(maps_webbrowser.DocumentText.ToString()); 
     maps_webbrowser.Document.InvokeScript("initialize"); 
    } 
}