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)
        {
        }
    }
相关推荐
米丘16 小时前
微前端之 Web Components 完全指南
微服务·html
Metaphor6923 天前
使用 Python 将 PDF 转换为 HTML
python·pdf·html
a1117764 天前
“黑夜流星“个人引导页 网页html
java·前端·html
JieE2124 天前
手把手带你用纯 CSS 实现一个 3D 旋转魔方,这些前端基础你能打几分?
前端·css·html
Chris _data4 天前
WPF 学习第三天 — Modbus RTU 串口通信
hadoop·学习·wpf
YHL4 天前
🧊 CSS 3D 硬核解析:四个属性手写旋转立方体
前端·css·html
a1117764 天前
无限森林漫游(简约几何版 html
前端·html
LaughingZhu4 天前
Product Hunt 每日热榜 | 2026-06-16
前端·人工智能·经验分享·chatgpt·html
布吉岛的石头4 天前
Java 程序员第 43 阶段05:微服务整合大模型,跨服务调用架构设计实战,Seata分布式事务实战
wpf
步步为营DotNet4 天前
基于.NET Aspire 实现云原生应用的高效监控与可观测性
云原生·.net·wpf