WPF中展示HTML

Winform的WebBrowser相对灵活一些。接下来把操作步骤分享给大家。

一、  引入dll

System.Windows.Forms.dll和WindowsFormsIntegration.dll。

两个都是.NET框架下的dll,不需要额外下载。

 

二、  向界面中添加定义的控件(将导入的dll引入到界面中啦)。Xaml代码部分:

xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

 

//插入Winform的WebBrowser控件

  <wfi:WindowsFormsHost Name="winFormsContainer">

  <wf:WebBrowser x:Name="webBrowser1"/>

  </wfi:WindowsFormsHost>

注意:只有在WindowsFormsHost标签下才能插入WinForm控件。当然你也可以插入其他WinForm控件

 

 三、cs代码段

           string html=“你需要展示的html代码”;

  

            //调用引入的webBrowser1

            webBrowser1.Navigate("about:blank");     //一定要创建一个空白界面。不然即使写入html成功显示也不能再次回读其中内容。

            webBrowser1.Document.OpenNew(false);

            webBrowser1.Document.Write(html);

            webBrowser1.Refresh();

 

             //这个时候你可以写js代码来操纵你想要的一切(如下)

             String newContent = webBrowser1.Document.GetElementById("container").InnerHtml;

             //也可以webBrowser写入html时加入一些css样式(真的很方便!)

猜你喜欢

转载自www.cnblogs.com/xuhongfei/p/8929891.html