roryok
roryok

Reputation: 9645

Detecting WebView Height via Scrolling in XAML

I really need to be able to work out how tall a piece of HTML is inside a given WebView. It's pretty much crucial to the project I'm trying to build. I know it's not immediately possible but I could determine whether a scrollbar existed on a WebView I might be able to increase the height of the view until it disappeared.

Any thoughts on this? Or any alternative solutions / suggestions? I looked into converting it for a TextBlock using the HTMLAgilityPack (now ported to Metro) but the HTML is too complex for that.

Upvotes: 3

Views: 2132

Answers (2)

jkulhanek
jkulhanek

Reputation: 307

This may be a good solution but there is still a problem with device pixel density. If you run your code on WP the conversion may look like this:

private double PixelsToLogicalPixels(double pixels)
{
     var info=Windows.Graphics.Display.DisplayInformation.GetForCurrentView();
     return pixels / info.RawPixelsPerViewPixel;
}
private double LogicalPixelsToPixels(double pixels)
{
     var info = Windows.Graphics.Display.DisplayInformation.GetForCurrentView();
     return pixels * info.RawPixelsPerViewPixel;
}

Upvotes: 0

Kieren Johnstone
Kieren Johnstone

Reputation: 42013

If you have control over the HTML, add this javascript:

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

(from http://james.padolsey.com/javascript/get-document-height-cross-browser/)

And call this rather than alert:

window.external.notify(getDocHeight());

Then implement the ScriptNotify event:

http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webview.scriptnotify.aspx

Upvotes: 8

Related Questions