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.
I have had to change the injection script to this:
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
Next thing, to calculate the body insides, i wrap it
Find the indexes from and to:
Next everything inside the wrap into .
Next, replace the old content with a new one ( that we created inside the wrapper ):
Next we need to attach 2 events to the WebView - DOMContentLoaded и ScriptNotify:
And here are the two event handlers: