我在System.Windows.Forms.WebBrowser
应用程序中使用WPF
(在单独的.dll文件中使用),以便在后台打印一些HTML
内容。
我的问题是HTML元素不受页面中javascript
的影响
我知道页面中没有错误,因为如果我在DocumentCompleted
事件之后复制其内容并将其粘贴到纯HTML文件中,则脚本可以正常工作。
我的HTML模板:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script>
function loaded() {
document.getElementById("content").innerHTML = "after javascript";
}
</script>
</head>
<body onload="loaded()">
<h1 id="content">before javascript</h1> <!--<= This value does not change-->
</body>
</html>
我按如下方式使用此HTML:
private void PrintTest()
{
string curDir = Directory.GetCurrentDirectory();
string fullPath = Path.Combine(curDir, "PrintDocs\\HTMLPage1.html");
string html = File.ReadAllText(fullPath);
HtmlPrint.Print(html);
}
public static class HtmlPrint
{
public static void Print(string html, string documentTitle = "")
{
var wb = new System.Windows.Forms.WebBrowser { DocumentText = html };
wb.DocumentCompleted += wb_DocumentCompleted;
}
private static void wb_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
var wb = ((System.Windows.Forms.WebBrowser)sender);
wb.Print();
wb.Dispose();
}
}
我在这里缺少什么?
答案 0 :(得分:1)
你忘记了onload中的大括号:
<body onload="loaded()">
答案 1 :(得分:1)
正如我在评论中提到的那样,您可以从C#代码中调用InvokeScript()
方法,并传入您应该执行此操作的js
函数名称。
wb.Document.InvokeScript("loaded");
答案 2 :(得分:0)
我认为您必须将onload="loaded"
更改为onload="loaded()"