我试图让我的WebView
在内容过宽而无滚动条的情况下自动缩小。
我已将WebView
包裹在ScrollBar
中,并根据ScrollViewers ZoomFactor
属性设置ViewPortWidth
除以实际网页宽度(通过JavaScript计算)
它几乎正常工作,但是在ScrollViewer缩小后,网页右侧的一部分没有显示 - 看起来缺少的部分对应于不可见的部分缩小了。这几乎就像网页在缩小时没有被重绘。
这是我的代码:
的.xaml
<Grid>
<ScrollViewer Name="WebScrollViewer">
<WebView x:Name="ContentView"
NavigationFailed="contentView_NavigationFailed"
DOMContentLoaded="contentView_LoadCompleted"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
</ScrollViewer>
</Grid>
的.cs
private void contentView_LoadCompleted(WebView sender, WebViewDOMContentLoadedEventArgs args)
{
// ask the content its width
var widthString = ContentView.InvokeScript("eval", new[] { "document.body.scrollWidth.toString()" });
int width;
if (!int.TryParse(widthString, out width))
throw new Exception(string.Format("failure/width:{0}", widthString));
// ask the content its height
var heightString = ContentView.InvokeScript("eval", new[] { "document.body.scrollHeight.toString()" });
int height;
if (!int.TryParse(heightString, out height))
throw new Exception(string.Format("failure/height:{0}", heightString));
if (WebScrollViewer.ViewportWidth < width)
{
// resize the webview to the content
ContentView.Width = width;
ContentView.Height = height;
var zoomFactor = WebScrollViewer.ViewportWidth / width;
WebScrollViewer.ZoomToFactor((float)zoomFactor);
WebScrollViewer.InvalidateScrollInfo();
}
}
我已经坚持了一段时间,并尝试了上述的各种变体,都有同样的问题。
我错过了一些明显的东西吗?或者有更好的方法来实现我想要做的事情吗?
答案 0 :(得分:0)
我想我终于把它排序了。 HorizontalScrollBarVisibility
的{{1}}默认值为ScrollViewer
。我想当你不能水平滚动并且ScrollViewer的内容比Disabled
更宽时,他们认为不需要呈现无法查看的内容。
所以,修复很容易,只需修改.xaml
即可ViewPort