WPF WebBrowser控件解析 HTML
xml
Window里面的AllowsTransparency属性不要加
xml
<WebBrowser x:Name="webBrowser" />
csharp
public void InitWeb()
{
string htmlString = @"<html>
<head>
<title>this is a test</title>
<script type ='text/javascript'>
function Hello()
{
window.external.Hello('hello test'); // 传递事件
}
</script>
</head>
<body>
<button onclick = 'Hello()'>
hello test
</button>
</body>
</html> ";
webBrowser.NavigateToString(htmlString);
ObjectForScriptingHelper helper = new ObjectForScriptingHelper(this);
webBrowser.ObjectForScripting = helper;
}
csharp
[System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问
public class ObjectForScriptingHelper
{
signature mainWindow;
public ObjectForScriptingHelper(signature main) // 注意signature 要写成你自己项目中的对象名
{
mainWindow = main;
}
//这个方法就是网页上要访问的方法
public void Hello(string cmd)
{
}
}