Use of CefSharp under WPF

Insert picture description here

I have been using WPF's own WebBrowsercontrols 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

  1. By Nuget installation, right-click the project -> Management Nuget Package -> Search in the open interface CefSharp, followed by the installation CefSharp.Commonand CefSharp.Wpf, as cef.redist.x64and cef.redist.x86installed automatically.

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-0DdYgWNU-1596456314747)(https://i.loli.net/2020/07/30/7NdrJnzxabOZfPh.png )]

  1. 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.

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-eEKdoUlV-1596456314751)(https://i.loli.net/2020/07/30/Unib4OjqVvwKB7P.png )]

use

When using, you can directly add ChromiumWebBrowsercontrols in the xaml file , but the ChromiumWebBrowsercontrols consume memory, so dynamic addition in the code is also a good choice.

Add a browser in xaml

  1. 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>
    
  2. Access URL of operation control in cs file:

    Browser.Load(“www.baidu.com”);
    

Dynamically add browsers

  1. 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));
        }
    }
    
  2. Dynamically add and operate controls:

    CollapsableChromiumWebBrowser Browser = new CollapsableChromiumWebBrowser();
    //页面插入控件
    ctrlBrowerGrid.Children.Add(Browser);
    //这里不能用Load()的方法,会报错。
    Browser.Address = “www.baidu.com”; 
    

Get Cookie and Html

  1. 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;
        }
    }
    
  2. Add Cookie and Html to get callback function

    public async void RecieveCookie(object data)
    {
        (string html,string cookies) = ((string,string))data;
        return;
    }
    
  3. 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;
    }
    

Guess you like

Origin blog.csdn.net/u014541617/article/details/107772258