cefSharp ChromiumWebBrowser大小到页面内容

时间:2017-05-29 18:44:24

标签: c# wpf cefsharp

你知道如何制作它,以便在打开ChromiumBrowser时,浏览器的大小是内容吗?现在,这个xaml可以在Window的主体中找到。当有人点击MyTextBox的文本时,ChromiumBrowser会打开以显示更多信息。

修改 没有固定的宽度和高度,铬窗口没有显示。

....
<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>
....

1 个答案:

答案 0 :(得分:-1)

我已经通过以下方式解决了使cefSharp浏览器高度适合页面内容的问题:

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