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)
        {
        }
    }
相关推荐
Jing_Rainbow14 分钟前
【 前端三剑客-39 /Lesson65(2025-12-12)】从基础几何图形到方向符号的演进与应用📐➡️🪜➡️🥧➡️⭕➡️🛞➡️🧭
前端·css·html
纆兰1 小时前
汇款单的完成
前端·javascript·html
二DUAN帝5 小时前
像素流与UE通信
前端·javascript·css·ue5·html·ue4·html5
jinmo_C++5 小时前
从零开始学前端 · HTML 基础篇(二):常用文本标签与排版基础
前端·html
Aevget6 小时前
DevExpress WPF中文教程:Data Grid - 如何绑定到有限制的自定义服务(四)?
wpf·界面控件·devexpress·ui开发·.net 10
3824278276 小时前
JS正则表达式实战:核心语法解析
开发语言·前端·javascript·python·html
阿呆5917 小时前
html前端开发注释的写法
前端·html
.又是新的一天.7 小时前
【前端Web开发HTML5+CSS3+移动web视频教程】02 html - 列表、表格、表单
前端·html·html5
冴羽8 小时前
2025 年 HTML 年度调查报告亮点速览!
前端·javascript·html
CQ_YM9 小时前
SQLite3 数据库与网页html
c语言·数据库·sqlite·html