cefSharp ChromiumWebBrowser size to page content
Asked Answered
E

1

7

Do you know how to make it so that when the ChromiumBrowser is opened, the browser sizes to contents? Right now this xaml is found in the body of a Window. When someone clicks on the text of MyTextBox, the ChromiumBrowser opens to show more information.

Edit: The chromium window doesn't show without the fixed width and height.

....
<Canvas Panel.ZIndex="99" Visibility="{Binding IsChromiuimVisible, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed}">
<Popup Placement="Top" PlacementTarget="{Binding ElementName=MyTextBlock}"
    IsOpen="{Binding IsChromiumVisible}" PopupAnimation="Fade" AllowsTransparency="True"
    MouseEnter="OnMouseEnter" MouseLeave="OnMouseLeave">
    <Grid Background="Transparent">
        <cefSharp:ChromiumWebBrowser
                 Width="300" Height="620"
              Address="{Binding ChromiumAddress, Mode=TwoWay}">
        </cefSharp:ChromiumWebBrowser>
    </Grid>
</Popup>
</Canvas>
....
Ezzell answered 29/5, 2017 at 18:44 Comment(3)
Remove the fixed width and heightTisman
The chromium window doesn't show anymore without the fixed width and height.Ezzell
Did you find a solution for this problem? I am facing a similar issue as well. But in my case the browser's content is always too stretched.Lobeline
P
5

I have solved the problem of fitting the cefSharp browser height to page content in the following way:

browser = new ChromiumWebBrowser("https://stackoverflow.com/");
// ... adding to parent's children etc. ...

browser.LoadingStateChanged += async (s, e) =>
{
    if (!e.IsLoading) // browser.CanExecuteJavascriptInMainFrame == TRUE !
    {
        JavascriptResponse response = 
            await browser.EvaluateScriptAsync(
                // GET HEIGHT OF CONTENT
                "(function() {                       " +
                "  var _docHeight =                  " +
                "    (document.height !== undefined) " +
                "    ? document.height               " +
                "    : document.body.offsetHeight;   " +
                "                                    " +
                "  return _docHeight;                " +
                "}                                   " +
                ")();");

        int docHeight = (int)response.Result;
        browser.Dispatcher.Invoke(() => { browser.Height = docHeight + 10; });
    }
};
Psychodynamics answered 16/8, 2020 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.