简体   繁体   English

在通用应用程序中打印

[英]Printing in Universal Apps

I'm developing a Windows 10 Universal App that needs to perform calculations and generate a printout based on the results. 我正在开发Windows 10通用应用程序,该应用程序需要执行计算并根据结果生成打印输出。 I don't know where to get started with printing. 我不知道从哪里开始打印。 What is the easiest way to create a new document? 创建新文档的最简单方法是什么? In WPF we had FixedDocument and FlowDocument. 在WPF中,我们有FixedDocument和FlowDocument。 I don't see those available in the Universal App format. 我看不到通用应用格式可用的那些。

Declare the PrintManager and PrintDocument . 声明PrintManagerPrintDocument The PrintManager type is in the Windows.Graphics.Printing namespace along with types to support other Windows printing functionality. PrintManager类型与其他支持Windows打印功能的类型一起位于Windows.Graphics.Printing命名空间中。 The PrintDocument type is in the Windows.UI.Xaml.Printing namespace along with other types that support preparing XAML content for printing. PrintDocument类型与其他支持准备XAML内容进行打印的类型一起位于Windows.UI.Xaml.Printing命名空间中。 You can make it easier to write your printing code by adding the following using or Imports statements to your page. 您可以通过在页面上添加以下使用或Imports语句来简化编写打印代码的过程。

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

The PrintDocument class is used to handle much of the interaction between the app and the PrintManager , but it exposes several callbacks of its own. PrintDocument类用于处理应用程序和PrintManager之间的大部分交互,但是它公开了一些自己的回调。 During registration, create instances of PrintManager and PrintDocument and register handlers for their printing events. 注册期间,创建PrintManagerPrintDocument实例,并为它们的打印事件注册处理程序。 In the UWP print sample, registration is performed by the RegisterForPrinting method. UWP打印示例中,注册是通过RegisterForPrinting方法执行的。

public virtual void RegisterForPrinting()
{
   printDocument = new PrintDocument();
   printDocumentSource = printDocument.DocumentSource;
   printDocument.Paginate += CreatePrintPreviewPages;
   printDocument.GetPreviewPage += GetPrintPreviewPage;
   printDocument.AddPages += AddPrintPages;

   PrintManager printMan = PrintManager.GetForCurrentView();
   printMan.PrintTaskRequested += PrintTaskRequested;
}

When the user goes to a page that supports, it initiates the registration within the OnNavigatedTo method. 当用户转到支持的页面时,它将在OnNavigatedTo方法内启动注册。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
   // Initalize common helper class and register for printing
   printHelper = new PrintHelper(this);
   printHelper.RegisterForPrinting();

   // Initialize print content for this scenario
   printHelper.PreparePrintContent(new PageToPrint());

   // Tell the user how to print
   MainPage.Current.NotifyUser("Print contract registered with customization, use the Print button to print.", NotifyType.StatusMessage);
}

When the user leaves the page, disconnect the printing event handlers. 当用户离开页面时,请断开打印事件处理程序的连接。 If you have a multiple-page app and don't disconnect printing, an exception is thrown when the user leaves the page and then comes back to it. 如果您有多页应用程序并且没有断开打印连接,则当用户离开页面然后返回页面时,将引发异常。

You can read more on Print from your app and also find the TIP: Tip Most of the examples in this topic are based on the print sample. 您可以从您的应用程序上阅读有关“ 打印”的更多信息,并找到提示: 提示本主题中的大多数示例均基于打印示例。 To see the full code, download the Universal Windows Platform (UWP) print sample from the Windows-universal-samples repo on GitHub. 要查看完整代码,请从GitHub上的Windows-universal-samples存储库下载通用Windows平台(UWP)打印示例。

GetPrintPreviewPage event is fierd once time for each page, when i return to a page the event not fierd. 对于每个页面,一次GetPrintPreviewPage事件被终止,当我返回页面时,该事件未被终止。 I use this code to persist the current preview page. 我使用此代码来保留当前的预览页面。

private async void PrintDoc_GetPreviewPage(object p_sender, GetPreviewPageEventArgs p_args) { SelectedPagePreview = p_args.PageNumber; 私有异步void PrintDoc_GetPreviewPage(object p_sender,GetPreviewPageEventArgs p_args){SelectedPagePreview = p_args.PageNumber;

i am testing on a windows 8.1 & tablet x64. 我正在Windows 8.1和平板电脑x64上进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM