I have been using WPF's own WebBrowser
controls before , but because it uses the IE kernel, it is difficult to use, and various errors are reported. So I changed to an open source browser package, CefSharp, which supports Winform and WPF, embeds the Chrome browser component and has more detailed documentation.
installation
- By Nuget installation, right-click the project -> Management Nuget Package -> Search in the open interface CefSharp, followed by the installation
CefSharp.Common
andCefSharp.Wpf
, ascef.redist.x64
andcef.redist.x86
installed automatically.
-
Configure the solution platform
Because CefSharp does not support ANYCPU, to configure x86, x64, click the menu Generate -> Configuration Manager. Select the solution platform, click Edit, delete x64 and x86 first, and then re-create it. Reconfiguration is easier.
use
When using, you can directly add ChromiumWebBrowser
controls in the xaml file , but the ChromiumWebBrowser
controls consume memory, so dynamic addition in the code is also a good choice.
Add a browser in xaml
-
Insert a reference in the header of the xmal file
xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
and add controls as follows:<Grid x:Name="ctrlBrowerGrid"> <wpf:ChromiumWebBrowser x:Name="Browser"/> </Grid>
-
Access URL of operation control in cs file:
Browser.Load(“www.baidu.com”);
Dynamically add browsers
-
Add browser class:
internal sealed class CollapsableChromiumWebBrowser : ChromiumWebBrowser { public CollapsableChromiumWebBrowser() { this.Loaded += this.BrowserLoaded; } private void BrowserLoaded(object sender, System.Windows.RoutedEventArgs e) { // Avoid loading CEF in designer if (DesignerProperties.GetIsInDesignMode(this)) return; // Avoid NRE in AbstractRenderHandler.OnPaint ApplyTemplate(); CreateOffscreenBrowser(new Size(400, 400)); } }
-
Dynamically add and operate controls:
CollapsableChromiumWebBrowser Browser = new CollapsableChromiumWebBrowser(); //页面插入控件 ctrlBrowerGrid.Children.Add(Browser); //这里不能用Load()的方法,会报错。 Browser.Address = “www.baidu.com”;
Get Cookie and Html
-
Add cookie access class
public class CookieVisitor : ICookieVisitor { public static string Cookies = null; public static string Html = null; public event Action<object> Action; public bool Visit(CefSharp.Cookie cookie, int count, int total, ref bool deleteCookie) { if(count == 0) Cookies = null; Cookies += cookie.Name + "=" + cookie.Value + ";"; deleteCookie = false; return true; } public void Dispose() { if (Action != null) Action((Html, Cookies)); return; } }
-
Add Cookie and Html to get callback function
public async void RecieveCookie(object data) { (string html,string cookies) = ((string,string))data; return; }
-
Browser control to access URL and set callback
async void LoadWebBrowser() { Browser.FrameLoadEnd += Browser_FrameLoadEnd; Browser.Address = "www.baidu.com"; } private async void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e) { CookieVisitor.Html = await Browser.GetSourceAsync(); CookieVisitor visitor = new CookieVisitor(); visitor.Action += RecieveCookie; Cef.GetGlobalCookieManager().VisitAllCookies(visitor); return; }