Search code examples
c#xamlwebviewwindows-store-appswindows-8.1

Windows Store WebView rendered html size


I have a strong need to calculate/get the rendered html size in WebView.

Is there a way to achieve this?

In Objective-C i could pass a HTML string to the WebView and available size, and it returned the desired Size.

Is there something similar to this in Windows Store WebView?

For example:

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

And based on desiredSize value i could resize my WebView to prevent scrolling inside the WebView, and fit all the content on XAML page.


Solution

  • Ok, i have came up with a solution:

    I injected this string into my html :

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

    That means insert it before

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

    Next thing, to calculate the body insides, i wrap it

    Find the indexes from and to:

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

    Next everything inside the wrap into .

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

    Next, replace the old content with a new one ( that we created inside the wrapper ):

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

    Next we need to attach 2 events to the WebView - DOMContentLoaded и ScriptNotify:

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

    And here are the two event handlers:

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