Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a ChromiumWebBrowser control with Visibility of Collapsed. Until the visibility is set to Visible, the page is not loaded and many calls fail with:

System.Exception: The browser has not been initialized. Load can only be called after the underlying CEF browser is initialized (CefLifeSpanHandler::OnAfterCreated).

How can I have the page be loaded when the Visibility is Collapsed?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
246 views
Welcome To Ask or Share your Answers For Others

1 Answer

Create a subclass of ChromiumWebBrowser as described in Render problems when Initial Visibility Collapsed. To work around a NullReferenceException in AbstractRenderHandler.OnPaint, you will need to ensure the visual tree is already created (by calling ApplyTemplate).

Example:

internal sealed class CollapsableChromiumWebBrowser : ChromiumWebBrowser
{
    public CollapsableChromiumWebBrowser()
    {
        this.Loaded += this.CollapsableChromiumWebBrowser_Loaded;
    }

    private void CollapsableChromiumWebBrowser_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        // Avoid loading CEF in designer
        if (DesignerProperties.GetIsInDesignMode(this))
        {
            return;
        }

        // Avoid NRE in AbstractRenderHandler.OnPaint
        ApplyTemplate();
        // https://github.com/cefsharp/CefSharp/issues/1412
        CreateOffscreenBrowser(new Size(400, 400));
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...