>为了支持分页,我使用< div style =“height:100vh”>生成第二个分成“页面”的HTML文档.对于每个“页面”,最多500个.
>我将“分页”HTML加载到XAML页面上的第二个隐藏的WebView中,我根据用户选择的页面大小调整大小以适合一页.
>我等待WebView完成加载……
>现在为每个“页面”:
>我将WebView的scrollY设置为仅使用JavaScript显示当前页面:window.scrollTo(0,-pageOffset)
>然后我使用WebViewBrush捕获WebView中当前页面的快照
>对所有剩余页面重复此操作…
问题:
我可以生成所有500页的打印预览,但有时预览会丢失页面而其他页面会多次显示.
我怀疑这是因为我使用WebViewBrush.Redraw()来捕获滚动的WebView的快照,但文档说Redraw()是异步发生的.我可以滚动浏览当前页面,然后WebViewBrush有机会重绘,因此意外捕获下一页.
如何确保WebViewBrush已捕获WebView以便我可以滚动到下一页?
private async Task<Rectangle> MakePage(WebView webView,Size pageSize,double pageOffset) { // Scroll to next page: await webView.EvaluateJavaScriptSnippetAsync( $"window.scrollTo(0,{pageOffset})"); var brush = new WebViewBrush(); brush.Stretch = Stretch.Uniform; brush.SetSource(webView); brush.Redraw(); // XXX Need to wait for this,but there's no API // Add a delay hoping Redraw() finishes... I think here is the problem. await Task.Delay(150); var rectangle = new Rectangle() { Width = pageSize.Width,Height = pageSize.Height }; rectangle.Fill = brush; brush.Stretch = Stretch.UniformToFill; brush.AlignmentY = AlignmentY.Top; return rectangle; }
注意:如果有使用WebViewBrush打印500页的替代方法,我可以提供建议.我尝试为每个页面使用单独的WebView,但应用程序在200页后耗尽内存.
BOUNTY:我开始向任何可以弄清楚如何打印500页的人提供100分的赏金.
WebViewBrush
remarks的重要部分:
A WebView control has an inherently asynchronous behavior that redraws the control when its content is completely loaded. But an associated WebViewBrush renders as soon as the XAML is parsed (which might be before the URI content is loaded by the WebView ). Alternatively,you can wait to call SetSource on the WebViewBrush until the source content is fully loaded (for example by calling SetSource in the handler for the WebView.LoadCompleted event.
这样你就可以在WebView.LoadCompleted
之后调用WebViewBrush的SetSource方法.