加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

如何等待WebViewBrush.Redraw()完成(UWP打印)?

发布时间:2020-12-14 04:06:52 所属栏目:Windows 来源:网络整理
导读:我有一个基本的UWP应用程序,内嵌WebView呈现相当大的 HTML文档(最多500个字母大小的打印页面). 我想添加对HTML文档打印的支持.这是我的方法: 为了支持分页,我使用 div style =“height:100vh”生成第二个分成“页面”的HTML文档.对于每个“页面”,最多500
我有一个基本的UWP应用程序,内嵌WebView呈现相当大的 HTML文档(最多500个字母大小的打印页面).

我想添加对HTML文档打印的支持.这是我的方法:

>为了支持分页,我使用< 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方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读