Detecting WebView Height via Scrolling in XAML

安稳与你 提交于 2019-12-12 14:52:35

问题


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.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/12727041/detecting-webview-height-via-scrolling-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!