0

I have a chromiumWebBrowser hosted in my application. user can navigate between html pages (with binging to address).

I need xaml scroller (not css), so I have a scrollViewer and inside the Chromium browser.

Every time the Address changes, chromium height needs to be full html doc height, to get the scrolling right.

I have tried setting it similar to answer in this Q: cefSharp ChromiumWebBrowser size to page content

which works well first time, but when navigating, it only grows - if first page is 600, and next is 200 - it returns 600 second time too.

XAML:

 <ScrollViewer x:Name="scroller"    >
            <wpf:ChromiumWebBrowser x:Name="chrome" 
                                Address="{Binding CurrentUrl}"  />
 </ScrollViewer>

C#:

chrome.LoadingStateChanged += async (s, e) =>
        {
            if (!e.IsLoading) // browser.CanExecuteJavascriptInMainFrame == TRUE !
            {
                JavascriptResponse response =
                    await chrome.EvaluateScriptAsync(
                        // GET HEIGHT OF CONTENT
                        $"(function()                     " +
                        "{  var _docHeight =                  " +
                        "    document.documentElement.scrollHeight;     " +
                        "                                    " +
                        "  return _docHeight;                " +
                        "}                                   " +
                        ")();");

                int docHeight = (int)response.Result;
                chrome.Dispatcher.Invoke(() => { chrome.Height = docHeight; });
            }
        };
  • Try using http://cefsharp.github.io/api/102.0.x/html/M_CefSharp_IWebBrowser_GetContentSizeAsync.htm to get the content size, make sure to await the method. – amaitland Jun 27 '22 at 19:40
  • @amaitland thanks, but it has same problem. first page is 200, second is 400, thirs should be 200 but stays 400... – user2111255 Jun 28 '22 at 07:39
  • I see in chrome devTools same behaviour - it you set the dimensions, and open another tab or browse - dimensions are not reseted. any idea how to reset dimensions of chrome? – user2111255 Jun 28 '22 at 09:28
  • Are you dealing with a responsive design? It's not uncommon for websites to adaptively resize to fill the space. I'd suggest updating your tags to include JavaScript as it's more relevant than say xaml. I don't have time to look into this. – amaitland Jun 28 '22 at 20:11
  • http://cefsharp.github.io/api/102.0.x/html/M_CefSharp_Handler_DisplayHandler_OnAutoResize.htm maybe worth testing, you need to explicitly enable auto resize. http://cefsharp.github.io/api/102.0.x/html/M_CefSharp_IBrowserHost_SetAutoResizeEnabled.htm create a class that inherits from DisplayHandler and override the method, assign a new instance to the browser DisplayHandler property. I don't know if it'll work in this case, worth a try. – amaitland Jul 01 '22 at 04:30

0 Answers0