Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

c# - How can I use a ChromiumWebBrowser when Collapsed

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

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

1 Reply

0 votes
by (71.8m points)

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));
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...