马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

UWP开发入门(二十四)—— Win10风格的打印对话框

[复制链接]
查看2192 | 回复1 | 2019-10-17 09:44:09 | 显示全部楼层 |阅读模式

虽然常常看到阿迪王发“看谁人开发UWP的又吊颈了”的图……照旧不由得重启一下这个系列。最近有用到UWP的print API,专程来写一篇给某软的这个伟大构想续一秒。

之前的打印对话框差不多长成这样:

094410bo427x2wre85xo7y.png

而新的Win10风格打印对话框是下图的样子,包罗预览图非常的直观。

094410gtce9q9l7byy33z3.png

起首让我们构建一个极简的UWP程序,太久没写的话说不定手都生了……

  1. <Grid>
  2. <Button Width="160" Height="80" Click="Button_Click" Background="Red">Print</Button>
  3. </Grid>
复制代码

我们试图在点击这个button时,通过PrintHelper类来体现打印对话框。

  1. private async void Button_Click(object sender, RoutedEventArgs e)
  2. {
  3. var printHelper = new PrintHelper();
  4. printHelper.PreparePrintContent(this);
  5. await printHelper.ShowPrintUIAsync();
  6. }
复制代码

到这里就是MainPage的全部内容了。然后让我们去看PrintHelper的实现。

在构造函数中,我们需要创建Printdocument和PrintManger的实例,用来注册打印相关的事故。

  1. public PrintHelper()
  2. {
  3. printDocument = new PrintDocument();
  4. printDocumentSource = printDocument.DocumentSource;
  5. //printDocument.Paginate += PrintDocument_Paginate;
  6. printDocument.GetPreviewPage += PrintDocument_GetPreviewPage;
  7. printDocument.AddPages += PrintDocument_AddPages;
  8. PrintManager printMan = PrintManager.GetForCurrentView();
  9. printMan.PrintTaskRequested += PrintMan_PrintTaskRequested;
  10. }
复制代码

PrintDocument是对即将打印文档的映射,我们接下来会通过它来构建预览试图等相关信息。

printDocument.Paginate事故主要用于准备全部的预览页,该事故会在打印对话框体现时,被实验一次。假如是单页的打印该事故不处理也可以。

printDocument.GetPreviewPage事故会在体现具体的预览页时,被实验。比方供两页内容,用户前后翻页预览时,每个预览页就是由这里设置。

Sample代码里由于只有一页,所以就直接将PrintContent赋值已往了。

  1. private void PrintDocument_GetPreviewPage(object sender, GetPreviewPageEventArgs e)
  2. {
  3. PrintDocument printDoc = (PrintDocument)sender;
  4. printDoc.SetPreviewPage(e.PageNumber, PrintContent);
  5. }
复制代码

printDocument.AddPages事故在现实打印操纵发生时被触发,printDocument会通过AddPage和AddPageComplete方法来通完成文档的准备,然后进行打印操纵。

  1. private void PrintDocument_AddPages(object sender, AddPagesEventArgs e)
  2. {
  3. PrintDocument printDoc = (PrintDocument)sender;
  4. printDoc.AddPage(PrintContent);
  5. printDoc.AddPagesComplete();
  6. }
复制代码

完成以上事故注册以后,我们来看PrintManger,这个可以明白为之前WPF中PrintDialog的UWP版本。我们终极通过它来启动UI打印对话框。根据文档,起首我们必须调用PrintManager.GetForCurrentView()方法,该方法将返回当前运动UWP Window关联的PrintManager,然后我们需要注册事故printMan.PrintTaskRequested,这个事故会在打印操纵发生时被触发。

  1. private void PrintMan_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
  2. {
  3. PrintTask printTask = null;
  4. printTask = args.Request.CreatePrintTask("1", sourceRequested =>
  5. {
  6. sourceRequested.SetSource(printDocumentSource);
  7. });
  8. }
复制代码

在这个事故里,一样平常会CreatePrintTask,然后做一些打印的设置,末了指定printDocumentSource。

PrintHelper里别的部分的代码,仅仅时简单的封装和参数传递:

  1. public async Task ShowPrintUIAsync()
  2. {
  3. if (PrintManager.IsSupported())
  4. {
  5. await PrintManager.ShowPrintUIAsync();
  6. }
  7. }
  8. public virtual void PreparePrintContent(UIElement printContent)
  9. {
  10. PrintContent = printContent;
  11. }
复制代码

具体大家可以参考Github上的Sample code:

https://github.com/manupstairs/UWPSamples/tree/master/UWPSamples/PrintingSample







来源:https://www.cnblogs.com/manupstairs/p/11688656.html
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则