首页 文章

Xamarin.Forms UWP - 打印Webview并提供分页支持

提问于
浏览
1

我正在研究基于Xamarin.Forms的项目,试图用分页打印webview内容 .

我已经提到了以下链接:How do I print WebView content in a Windows Store App?

但不幸的是,这种方式不适用于Xamarin.Forms,因为在上面的链接中演示的方式是使用webviewbrush填充矩形(窗口形状)(矩形和webviewbrush都是平台相关控件到UWP) . 问题是我们无法使用webviewbrush绘制矩形(Xamarin Forms形状) .

作为一种解决方法,我做了以下事情:

  • 在xamarin表单PCL项目中创建了一个boxview

  • 在UWP项目中为这个boxview创建了一个渲染器(这将为我们提供windows矩形形状)然后我将这个矩形形状放入PCL项目的App.cs类中的一个公共静态属性中,以使其可用于Webviewrenderer.cs用webviewbrush填充此矩形的类 .

  • 我可以从UWP项目的webviewrenderer.cs类访问它,但问题是打印机对话框显示一个空页面 .

  • 分页工作正常,如上面的堆栈溢出链接所示,但所有页面都是空的 .

显然问题是矩形 . 因为如果我创建一个本机UWP项目并且打印机对话框也显示网页,那么相同的逻辑工作正常 .

任何帮助将非常感激 .

提前致谢!

1 回答

  • 0

    分页工作正常,如上面的堆栈溢出链接所示,但所有页面都是空的 .

    我已根据您的流程实现了基本打印功能 . 我将 GetWebViewBrush 方法放在 webviewrenderer 中 . 不幸的是,同样的问题出现在我身边 .

    async Task<WebViewBrush> GetWebViewBrush(Windows.UI.Xaml.Controls.WebView webView)
    {
        // resize width to content
        var _OriginalWidth = webView.Width;
        var _WidthString = await webView.InvokeScriptAsync("eval",
            new[] { "document.body.scrollWidth.toString()" });
        int _ContentWidth;
    
        if (!int.TryParse(_WidthString, out _ContentWidth))
            throw new Exception(string.Format("failure/width:{0}", _WidthString));
    
        webView.Width = _ContentWidth;
    
        // resize height to content
        var _OriginalHeight = webView.Height;
    
        var _HeightString = await webView.InvokeScriptAsync("eval",
            new[] { "document.body.scrollHeight.toString()" });
        int _ContentHeight;
    
        if (!int.TryParse(_HeightString, out _ContentHeight))
            throw new Exception(string.Format("failure/height:{0}", _HeightString));
    
        webView.Height = _ContentHeight;
    
        // create brush
        var _OriginalVisibilty = webView.Visibility;
    
        webView.Visibility = Windows.UI.Xaml.Visibility.Visible;
    
        var _Brush = new WebViewBrush
        {
            Stretch = Stretch.Uniform,
            SourceName = webView.Name
        };
      //  _Brush.SetSource(webView);
    
        _Brush.Redraw();
    
        // reset, return
        webView.Width = _OriginalWidth;
        webView.Height = _OriginalHeight;
        webView.Visibility = _OriginalVisibilty;
        return _Brush;
    }
    

    当我进行调试时,我发现 webView.Name 从未设置过值,所以我为customWebView创建了一个新的 Name 属性 .

    public static readonly BindableProperty NameProperty = BindableProperty.Create(
     propertyName: "Name",
     returnType: typeof(string),
     declaringType: typeof(MyWebView),
     defaultValue: default(string));
    
         public string Name
         {
             get { return (string)GetValue(NameProperty); }
             set { SetValue(NameProperty, value); }
         }
    
     }
    

    并使用 OnElementChanged 方法中的以下代码设置本机控件的 Name 属性( Windows.UI.Xaml.Controls.WebView ) .

    if (e.NewElement != null)
    {
        Control.Name = (Element as MyWebView).Name;
        Control.NavigationCompleted += Control_NavigationCompleted;
    }
    

    令人惊讶的是,页面不再是空的 .
    enter image description here

相关问题