The code requires CSHTML5 v1.0 Beta 7.2 or newer.
To use, simply add a new class named "PrintHelper.cs" to your project, and copy/paste the following code:
Code: Select all
using System;
using System.Windows;
using Windows.UI.Xaml;
namespace CSHTML5.Extensions.Printing
{
public static class PrintHelper
{
public static void Print(FrameworkElement frameworkElement)
{
if (CSharpXamlForHtml5.DomManagement.IsControlInVisualTree(frameworkElement))
{
if (!CSHTML5.Interop.IsRunningInTheSimulator)
{
var htmlDiv = CSHTML5.Interop.GetDiv(frameworkElement);
CSHTML5.Interop.ExecuteJavaScript(@"
var printPreview = window.open('about:blank', 'print_preview', 'resizable=yes,scrollbars=yes,status=yes');
var printDocument = printPreview.document;
printDocument.open();
printDocument.write('<!DOCTYPE html>'+
'<html>'+
$0.innerHTML+
'</html>');
printDocument.close();
printPreview.print();", htmlDiv);
}
else
{
MessageBox.Show("Printing is not available when running inside the Simulator.");
}
}
else
{
throw new Exception("You can only print an element that is in the visual tree. To fix the issue, add the FrameworkElement to the visual tree before printing it.");
}
}
}
}
How to use?
To use the class, just call the method "CSHTML5.Extensions.Printing.PrintHelper.Print(frameworkElement)", passing the FrameworkElement that you want to print.
How does it work internally?
The class works by copying the portion of the HTML DOM that corresponds to your FrameworkElement into a new browser window, and then calling the "Print Dialog" of the browser.
JS-Support