(WPF) Force page loading and rendering, even if browser is not visible

3,475 views
Skip to first unread message

tharibo

unread,
Jun 4, 2015, 5:14:35 AM6/4/15
to cefs...@googlegroups.com

I'm trying to cache the rendering of web pages by using multiple stacked ChromiumWebBrowser instances:
<TabControl x:Name="tabs" Grid.Row="2" Grid.ColumnSpan="4" Margin="5" >
    <TabItem>
        <TabItem.Header>
            <Label Visibility="Collapsed"></Label>
        </TabItem.Header>
        <cefSharp:ChromiumWebBrowser FrameLoadStart="webBrowser_FrameLoadStarted" FrameLoadEnd="webBrowser_LoadCompleted" />
    </TabItem>
    <TabItem>
        <TabItem.Header>
            <Label Visibility="Collapsed"></Label>
        </TabItem.Header>
        <cefSharp:ChromiumWebBrowser FrameLoadStart="webBrowser_FrameLoadStarted" FrameLoadEnd="webBrowser_LoadCompleted" />
    </TabItem>
    <TabItem>
        <TabItem.Header>
            <Label Visibility="Collapsed"></Label>
        </TabItem.Header>
        <cefSharp:ChromiumWebBrowser FrameLoadStart="webBrowser_FrameLoadStarted" FrameLoadEnd="webBrowser_LoadCompleted" />
    </TabItem>
    <TabItem>
        <TabItem.Header>
            <Label Visibility="Collapsed"></Label>
        </TabItem.Header>
        <cefSharp:ChromiumWebBrowser FrameLoadStart="webBrowser_FrameLoadStarted" FrameLoadEnd="webBrowser_LoadCompleted" />
    </TabItem>
    <TabItem>
        <TabItem.Header>
            <Label Visibility="Collapsed"></Label>
        </TabItem.Header>
        <cefSharp:ChromiumWebBrowser FrameLoadStart="webBrowser_FrameLoadStarted" FrameLoadEnd="webBrowser_LoadCompleted" />
    </TabItem>
</TabControl>

(this is inside a grid).

While I am able to change the Address of each browser before showing it, the actual rendering only takes place once the browser is visible.
Is there a way to force the rendering in the background?

I already tried to use this method:
public static class ExtensionMethods
{
    private static Action EmptyDelegate = delegate() { };
    public static void Refresh(this UIElement uiElement)
    {
        uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
    }
}


(I am new to WPF so maybe I'm not seeing something that should be obvious).

Alex Maitland

unread,
Jun 4, 2015, 5:32:53 AM6/4/15
to cefs...@googlegroups.com
Not sure that's possible at moment. You'd have to expose the `WasHidden` method

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf/ChromiumWebBrowser.cs#L886

tharibo

unread,
Jun 4, 2015, 10:43:56 AM6/4/15
to cefs...@googlegroups.com
So I tried to expose and call the method, but it doesn't work. It doesn't work because Window Rendering is disabled. (I exposed IsWindowRenderingIsDisabled method).
Is this because we are not in CefClient but in Wpf ChromiumWebBrowser?

On the same vein, I tried to call Invalidate, but it uses the same kind of code :
cef_browser.h:


 ///
 
// Notify the browser that it has been hidden or shown. Layouting and
 
// CefRenderHandler::OnPaint notification will stop when the browser is
 
// hidden. This method is only used when window rendering is disabled.
 
///
 
/*--cef()--*/
 
virtual void WasHidden(bool hidden) =0;


 
///
 
// Invalidate the view. The browser will call CefRenderHandler::OnPaint
 
// asynchronously. This method is only used when window rendering is
 
// disabled.
 
///
 
/*--cef()--*/
 
virtual void Invalidate(PaintElementType type) =0;

Alex Maitland

unread,
Jun 4, 2015, 10:01:28 PM6/4/15
to cefs...@googlegroups.com
That method is only valid in the context of offscreen rendering (window rendering disabled).

The underlying `CEF` browser is only created once the control gets width/height. So it's probably not actually created the instance in your scenario.

You'll have to debug to find out what exactly is going on.

tharibo

unread,
Jun 5, 2015, 2:22:00 AM6/5/15
to cefs...@googlegroups.com
That's what I thought.

I quickly tried to set a fixed size to my ChromiumWebBrowsers but it doesn't resolve the problem by itself.

Is it possible to debug CEF from a CefSharp project? How?

tharibo

unread,
Jun 16, 2015, 11:00:14 AM6/16/15
to cefs...@googlegroups.com
I finally got it to work.
Nothing to do with either CEF or CefSharp.
The WPF TabControl was the problem. It doesn't render its children until actually shown, or something close.
I replaced it with a simple Grid (no row, no column) and placed all my browsers on top of each other.
I manage the top one by using Canvas.SetZIndex();

        <Grid x:Name="tabs" Grid.Row="2" Grid.ColumnSpan="4" Margin="5" >
                <cefSharp:ChromiumWebBrowser x:Name="browser0" />
                <cefSharp:ChromiumWebBrowser x:Name="browser1" />
                <cefSharp:ChromiumWebBrowser x:Name="browser2" />
                <cefSharp:ChromiumWebBrowser x:Name="browser3" />
                <cefSharp:ChromiumWebBrowser x:Name="browser4" />
        </Grid>

reaed me

unread,
Mar 14, 2017, 4:39:35 AM3/14/17
to CefSharp
Hi how can we do this in Winforms any solution. Thanks
Reply all
Reply to author
Forward
0 new messages