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)
        {
        }
    }
相关推荐
武藤一雄3 小时前
WPF/C# 应对消息洪峰与数据抖动的 8 种“抗压”策略
windows·微软·c#·wpf·.netcore·防抖·鲁棒性
spencer_tseng4 小时前
HTML5 - Android - IOS
android·ios·html·html5
坊钰4 小时前
SpringBean的生命周期
前端·html
饼干哥哥18 小时前
2026年是 AI落地大年,但会淘汰一批AI PPT工具
html
酉鬼女又兒19 小时前
零基础快速入门前端Web存储(sessionStorage & localStorage)知识点详解与蓝桥杯考点应用(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯·html
apcipot_rain1 天前
HTML知识概述
前端·javascript·html
武藤一雄1 天前
WPF深度解析Behavior
windows·c#·.net·wpf·.netcore
Maybe_ch1 天前
WPF的STA线程模型、APM与TAP:从线程约束到现代异步
c#·.net·wpf
yivifu1 天前
接近完美的HTML文本双行合一排版
前端·javascript·html·双行合一