【问题标题】:Windows Store WebView rendered html sizeWindows 应用商店 WebView 呈现的 html 大小
【发布时间】:2015-03-18 16:47:20
【问题描述】:

我非常需要在 WebView 中计算/获取呈现的 html 大小。

有没有办法做到这一点?

在 Objective-C 中,我可以将 HTML 字符串传递给 WebView 和可用大小,然后它返回所需的大小。

Windows Store WebView 中是否有类似的东西?

例如:

WebView webView = new WebView();
webView.NavigateToString(html);
webView.ContentLoaded+= webView_ContentLoaded(,);

webView_ContentLoaded(,)
{
    //Height=99999999 - does the trick, like available height is infinite
    //Width=200 - means that width is limited to 200px
    Size desiredSize = webView.Content.Measure(new Size{Height=99999999, Width=200}());
}

根据desiredSize 值,我可以调整我的WebView 的大小以防止在WebView 内滚动,并适合XAML 页面上的所有内容。

【问题讨论】:

    标签: c# xaml webview windows-store-apps windows-8.1


    【解决方案1】:

    好的,我想出了一个解决方案:

    我将此字符串注入到我的 html 中:

    string injectionStyleAndScript = @"html,body {margin: 0;padding: 0;height: 100%;}function getHeight() { var height = document.getElementById('wrapper').offsetHeight; window.external.notify (''+高度);}";

    也就是说在之前插入

    htmlFragment – this is HTMl that i had to place on my WebView.
    htmlFragment = htmlFragment.Insert(htmlFragment.IndexOf("</HEAD>"),injectionStyleAndScript); // Inser the Injection
    

    接下来,计算身体内部,我把它包起来

    查找从和到的索引:

    int from = htmlFragment.IndexOf("<BODY>")+6;
    int to = htmlFragment.IndexOf("</BODY>");
    

    接下来将包裹内的所有内容放入 .

    string bodyWrapper = string.Format("<div id='wrapper'>{0}</div>", htmlFragment.Substring(from, (htmlFragment.Length - 1 - from)-(htmlFragment.Length - 1 - to)));
    

    接下来,将旧内容替换为新内容(我们在包装器中创建的内容):

    //Cut out the old one
    htmlFragment = htmlFragment.Remove(from, (htmlFragment.Length - 1 - from) - (htmlFragment.Length - 1 - to));
    //Insert our wrapper
    htmlFragment = htmlFragment.Insert(from, bodyWrapper);
    //Navigate to the HTML
    MyWebView.NavigateToString(htmlFragment);
    

    接下来我们需要将 2 个事件附加到 WebView - DOMContentLoaded и ScriptNotify:

    MyWebView.ScriptNotify += MyWebView;
    MyWebView.DOMContentLoaded += MyWebView;
    MyWebView.NavigateToString(htmlFragment);
    

    这是两个事件处理程序:

    void MyWebView_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
    {
        MyWebView.InvokeScriptAsync("getHeight", null);
    }
    
    void MyWebView_ScriptNotify(object sender, NotifyEventArgs e)
    {
        string result = e.Value.ToString();
        MyWebView.Height = double.Parse(result);
    }
    

    【讨论】:

    • 我在MyWebView.InvokeScriptAsync("getHeight", null); 线收到System.Exception
    • @KinjanBhavsar 无法真正帮助您,因为我没有看到您的代码。
    • @Cheese 设置高度后是否重绘了 webview?除了改变高度之外,我已经完成了所有工作。
    • 你摇滚!通过将 IndexOf 结果与 -1 进行比较并搜索 HTML 标记的小写版本,我使它变得更加健壮,但仍然......您的答案修复了我的概念验证应用程序中的一个非常严重的错误正在努力。非常感谢!
    【解决方案2】:

    我不得不将注入脚本更改为:

    string injectionStyleAndScript = @"<style> html,body {margin: 0;padding: 0;height: 100%;} </style><script type='text/javascript'>function getHeight() { var height = document.getElementById('wrapper').offsetHeight; window.external.notify(''+height);}</script>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多