b85411
b85411

Reputation: 10000

UWP - print webview

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:

Any help is greatly appreciated. Thanks.

Upvotes: 5

Views: 3045

Answers (1)

Elvis Xia - MSFT
Elvis Xia - MSFT

Reputation: 10831

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.

Upvotes: 5

Related Questions