Xamarin.Forms UWP - Print Webview with pagination support

一笑奈何 提交于 2019-12-11 07:44:06

问题


I'm working on Xamarin.Forms based project, trying to print the webview content with pagination.

I've referred the following link already: How do I print WebView content in a Windows Store App?

But unfortunately this way is not working with Xamarin.Forms because the way demonstrated in the above link is by filling the rectangle(windows shape) using webviewbrush (both the rectangle and webviewbrush are platform dependent controls to UWP). The problem is we can't use webviewbrush to draw rectangle(Xamarin Forms shape).

As a workaround I did the following:

  • created a boxview in xamarin forms PCL project
  • created a renderer for this boxview in UWP project(this will give us the windows rectangle shape) and then I did put this rectangle shape into one of the public static properties in PCL project's App.cs class to make it available for Webviewrenderer.cs class for filling this rectangle with webviewbrush.
  • I can able to access it from webviewrenderer.cs class from UWP project but the problem is the printer dialog shows an empty page.
  • Pagination works just fine as demonstrated in the above stack overflow link, but all pages are being empty.

Apparently the problem would be with rectangle. Because the same logic is just works fine if I create a native UWP project and the printer dialog shows the webpage as well.

Any help would be much appreciated.

Thanks in advance!


回答1:


Pagination works just fine as demonstrated in the above stack overflow link, but all pages are being empty.

I have realized the basic printing feature according to your process. I placed the GetWebViewBrush method in the webviewrenderer. Unfortunately, the same issue occur in my side.

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

When I was debuging, I found webView.Name has never been set value, So I make a new Name property for the customWebView.

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

 }

And set the Name property of native control(Windows.UI.Xaml.Controls.WebView) with the following code in OnElementChanged method.

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

Surprisingly, the page is no longer empty.



来源:https://stackoverflow.com/questions/46203311/xamarin-forms-uwp-print-webview-with-pagination-support

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