WPF WebBrowser控件解析 HTML

WPF WebBrowser控件解析 HTML

Window里面的AllowsTransparency属性不要加
新增的css样式不识别
<WebBrowser x:Name="webBrowser"  /> <!--IE8-->
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;
}

    [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问
    public class ObjectForScriptingHelper
    {
    
    
        signature mainWindow;

        public ObjectForScriptingHelper(signature main) // 注意signature 要写成你自己项目中的对象名
        {
    
    
            mainWindow = main;
        }

        //这个方法就是网页上要访问的方法
        public void Hello(string cmd)
        {
    
    
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_45381071/article/details/139231086