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)
        {
        }
    }
相关推荐
anOnion1 天前
构建无障碍组件之Radio group pattern
前端·html·交互设计
前端Hardy1 天前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
Scout-leaf2 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
前端Hardy3 天前
HTML&CSS&JS:基于定位的实时天气卡片
javascript·css·html
前端Hardy3 天前
HTML&CSS:纯CSS实现随机转盘抽奖机——无JS,全靠现代CSS黑科技!
css·html
DeathGhost3 天前
分享URL地址到微信朋友圈没有缩略图?
前端·html
前端Hardy3 天前
HTML&CSS:高颜值产品卡片页面,支持主题切换
css·html
Never_Satisfied4 天前
在HTML & CSS中,nth-child、nth-of-type详解
前端·css·html
你怎么知道我是队长4 天前
前端学习---HTML---块元素和行内元素
前端·学习·html
holeer4 天前
【V1.0】Typora 中的 HTML 支持|软件文档自翻译
前端·编辑器·html·typora·web·markdown·文档