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)
        {
        }
    }
相关推荐
脚踏实地皮皮晨31 分钟前
003003002_WPF Grid 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio
IMPYLH1 天前
HTML 的 <dt> 元素
java·前端·html
脚踏实地皮皮晨1 天前
003002004_WPF Panel 基类 官方类定义
开发语言·windows·算法·c#·wpf·visual studio
IMPYLH1 天前
HTML 的 <em> 元素
java·前端·html
a1117761 天前
农业数字孪生大屏网页 html开源
前端·html
湿滑路面1 天前
Rouyan:使用WPF/C#构建的基于LLM的快捷翻译小工具
开发语言·c#·wpf
软萌萌的12 天前
C#中的多级缓存架构设计与实现深度解析
缓存·c#·wpf
a1117762 天前
汽车3D配置器 THreeJS 开源项目
前端·3d·html·汽车
dalong102 天前
WPF:3D正八面体自动旋转
3d·wpf
醉城夜风~2 天前
HTML列表标签学习博客:有序列表、无序列表、自定义列表详解
前端·学习·html