WPF WebBrowser控件解析 HTML

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)
        {
        }
    }
相关推荐
anOnion15 小时前
构建无障碍组件之Switch Pattern
前端·html·交互设计
willow6 天前
html5基础整理
html
anOnion9 天前
构建无障碍组件之Radio group pattern
前端·html·交互设计
前端Hardy9 天前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
Scout-leaf9 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
前端Hardy10 天前
HTML&CSS&JS:基于定位的实时天气卡片
javascript·css·html
前端Hardy10 天前
HTML&CSS:纯CSS实现随机转盘抽奖机——无JS,全靠现代CSS黑科技!
css·html
DeathGhost10 天前
分享URL地址到微信朋友圈没有缩略图?
前端·html
前端Hardy11 天前
HTML&CSS:高颜值产品卡片页面,支持主题切换
css·html
Never_Satisfied11 天前
在HTML & CSS中,nth-child、nth-of-type详解
前端·css·html