Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP - print webview

Tags:

c#

webview

uwp

I want to print a WebView contents inside a UWP app.

I've set up my WebView to accept an HTML string, and this works fine:

    <WebView
        ext:HtmlExtension.HtmlString="{Binding HtmlString}"
        x:Name="MyWebView"
        Grid.Row="1"
        Grid.Column="0"
    />

I noticed this though on MSDN:

A WebView that hosts content off the UI thread is not compatible with parent controls that require gestures to propagate up from the WebView control to the parent, such as FlipView, ScrollViewer, and other related controls. These controls will not be able to receive gestures initiated in the off-thread WebView. In addition, printing off-thread web content is not directly supported – you should print an element with WebViewBrush fill instead.

Now I'm completely confused.

Can someone please explain how I can:

  • Create a WebViewBrush given I have a WebView
  • How to then print this WebViewBrush

Any help is greatly appreciated. Thanks.

like image 277
b85411 Avatar asked Aug 18 '16 07:08

b85411


1 Answers

You can use the following codes to create a WebViewBrush:

WebViewBrush wvBrush = new WebViewBrush();
wvBrush.SetSource(myWebView);
wvBrush.Redraw();
myRect.Fill = wvBrush;

XAML:

<WebView Name="myWebView" Source="http://www.google.com" Width="1024" Height="500" LoadCompleted="myWebView_LoadCompleted"/>
<Rectangle Name="myRect" Width="1024" Height="500"/>

Then the static content of the webview will be rendered into the Rechtangle just like a picture. And you can print the Rechtangle instead.

For printing, I suggest you reading through Print from your app. And there is also an official sample for you to refer to:UWP Print Sample.

like image 160
Elvis Xia - MSFT Avatar answered Oct 17 '22 04:10

Elvis Xia - MSFT