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)
        {
        }
    }
相关推荐
ycydynq11 小时前
python html 解析的一些写法
linux·python·html
廾匸6401 天前
语义化标签
前端·javascript·html
BBB努力学习程序设计1 天前
用Bootstrap一天搞定响应式网站:前端小白的救命稻草
前端·html
灵犀坠1 天前
前端开发核心知识:HTML5特性与经典面试题详解
前端·html·html5
程序猿_极客1 天前
【期末网页设计作业】HTML+CSS+JS 旅行社网站、旅游主题设计与实现(附源码)
javascript·css·html·课程设计·期末网页设计
Macbethad1 天前
使用WPF编写一个读取串口的程序
wpf
q***58191 天前
【HTML+CSS】使用HTML与后端技术连接数据库
css·数据库·html
晓得迷路了2 天前
栗子前端技术周刊第 106 期 - pnpm 10.21、Node.js v25.2.0、Bun v1.3.2...
前端·javascript·html
一颗宁檬不酸2 天前
页面布局练习
前端·html·页面布局
BBB努力学习程序设计2 天前
响应式页面设计与实现:让网站适配所有设备的艺术
前端·html