Displaying HTML in WPF

Winform's WebBrowser is relatively flexible. Next, I will share the steps with you.

First, the introduction of dll

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

Both are dlls under the .NET framework and do not require additional downloads.

 

2. Add the defined controls to the interface (introduce the imported dll into the interface). Xaml code part:

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

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

 

//Insert the WebBrowser control of Winform

  <wfi:WindowsFormsHost Name="winFormsContainer">

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

  </wfi:WindowsFormsHost>

Note: WinForm controls can only be inserted under the WindowsFormsHost tag. Of course you can also insert other WinForm controls

 

 Three, cs code segment

           string html="the html code you need to display";

  

            //Call the imported webBrowser1

            webBrowser1.Navigate("about:blank"); //Be sure to create a blank interface. Otherwise, even if the html is written and displayed successfully, the content cannot be read back again.

            webBrowser1.Document.OpenNew(false);

            webBrowser1.Document.Write(html);

            webBrowser1.Refresh ();

 

             //This time you can write js code to manipulate everything you want (below)

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

             //You can also add some css styles when webBrowser writes html (really very convenient!)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768160&siteId=291194637