微软在Win10时代终于完成的设备系统的大统一,"56个民族,56支花……"(⊙o⊙)…,既然统一了,那么也就意味着API也统一了,所以在UWP中,我们就可以使用统一的打印API来为设备(包括移动设备)添加基于XAML的App打印功能。使用Windows.Graphics.Printing和Windows.UI.Xaml.Printing命名空间,就可以很方便的将打印功能添加到我们的应用中。

下面是个例子,我们需要打印一个购物清单,前台代码如下:

 <Page.Resources>
<SolidColorBrush x:Key="MainColor" Color="#0067a6"/>
<SolidColorBrush x:Key="LightColor" Color="#00abd8"/>
<SolidColorBrush x:Key="buttonColor" Color="#f29c9c"/>
</Page.Resources>
<Page.BottomAppBar>
<CommandBar>
<AppBarButton x:Name="appbar_Printer" Label="打印" Click="appbar_Printer_Click">
<AppBarButton.Icon>
<BitmapIcon UriSource="Images/Printer.png"/>
</AppBarButton.Icon>
</AppBarButton>
<AppBarButton Icon="Contact" Label="客服"/>
</CommandBar>
</Page.BottomAppBar> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel x:Name="sp_PrintArea">
<StackPanel HorizontalAlignment="Center">
<TextBlock Foreground="{StaticResource MainColor}" Text="购物清单"/>
</StackPanel>
<StackPanel >
<TextBlock Text="订单ID:G20150618094144234"/>
<Image Margin="0,6" Source="Images/surface pro 3.PNG" />
<TextBlock Margin="0,6" Text="商品名称:Surface Pro3 I7(8G 512G) 中国版"/>
<TextBlock Margin="0,6" Text="数量:1"/>
<TextBlock Margin="0,6" Text="单价:13,988¥"/>
<Rectangle Margin="0,6" Height="1" Fill="{StaticResource LightColor}"/>
</StackPanel>
<StackPanel HorizontalAlignment="Center">
<TextBlock Foreground="{StaticResource MainColor}" Text="收货人信息"/>
</StackPanel>
<StackPanel >
<TextBlock Margin="0,6" Text="收货人:Aran"/>
<TextBlock Margin="0,6" Text="联系电话:186****0081"/>
<TextBlock Margin="0,6" Text="地址:地球村 中国小分队 xxx号"/>
</StackPanel>
</StackPanel>
</Grid>

需要用到的类:

  • PrintManager:PrintManage 打印机管理器是负责为Windows应用程序安排打印流,使用时必须调用PrintManager.GetForCurrentView()方法来返回特定于当前窗口的PrintManager
  • PrintDocument:PrintDocument 将打印流发送到打印机的一个可重用对象
  • PrintTask:表示包括要打印的内容以及提供对描述将如何打印内容的信息的访问的打印操作
  • PrintManager.PrintTaskRequested: 当发生打印的请求时,通过 ShowPrintUIAsync方法通过编程方式调用打印,或通过用户操作可能会触发此事件
  • PrintTaskRequest. CreatePrintTask():创建打印任务
  • PrintTask.Completed:打印完成事件
  • PrintDocument.OnGetPreviewPage: 订阅打印预览事件
  • PrintDocument.Paginate: 订阅预览时 打印参数发生变化事件 比如文档方向
  • PrintDocument.AddPages: 添加页面处理事件
  • PrintDocument.AddPage(UIElement pageVisual):添加打印页面,需要UI元素参数
  • PrintDocument. SetPreviewPage():设置要预览的页面

后台代码实现:

 public sealed partial class MainPage : Page
{
/// <summary>
/// 使用PrintManage.GetForCurrentView()获取一个PrintManager对象
/// PrintManage 打印机管理器是负责为Windows应用程序安排打印流
/// 使用时必须调用PrintManager.GetForCurrentView()方法来返回特定于当前窗口的PrintManager
/// </summary>
PrintManager printmgr = PrintManager.GetForCurrentView(); /// <summary>
/// PrintDocument 将打印流发送到打印机的一个可重用对象
/// </summary>
PrintDocument printDoc = null; /// <summary>
/// RotateTransform 是来旋转打印元素的,如果设备是横着的则需要旋转90°
/// </summary>
RotateTransform rottrf = null; /// <summary>
/// 表示包括要打印的内容以及提供对描述将如何打印内容的信息的访问的打印操作
/// </summary>
PrintTask task = null; public MainPage()
{
this.InitializeComponent();
//当发生打印的请求时,通过 ShowPrintUIAsync方法通过编程方式调用打印,或通过用户操作可能会触发此事件
printmgr.PrintTaskRequested += Printmgr_PrintTaskRequested;
} private void Printmgr_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
//从参数的Request属性中获取与PrintTaskRequest的任务关联
//创建好打印内容和任务后 在调用Complete方法进行打印
var deferral = args.Request.GetDeferral();
// 创建打印任务
task = args.Request.CreatePrintTask("购物信息-打印单", OnPrintTaskSourceRequrested);
task.Completed += PrintTask_Completed;
deferral.Complete();
} private void PrintTask_Completed(PrintTask sender, PrintTaskCompletedEventArgs args)
{
//打印完成
} private async void OnPrintTaskSourceRequrested(PrintTaskSourceRequestedArgs args)
{
var def = args.GetDeferral();
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
// 设置打印源
args.SetSource(printDoc?.DocumentSource);
});
def.Complete();
} private async void appbar_Printer_Click(object sender, RoutedEventArgs e)
{
if (printDoc != null)
{
printDoc.GetPreviewPage -= OnGetPreviewPage;
printDoc.Paginate -= PrintDic_Paginate;
printDoc.AddPages -= PrintDic_AddPages;
}
this.printDoc = new PrintDocument();
//订阅预览事件
printDoc.GetPreviewPage += OnGetPreviewPage;
//订阅预览时 打印参数发生变化事件 比如文档方向
printDoc.Paginate += PrintDic_Paginate;
//添加页面处理事件
printDoc.AddPages += PrintDic_AddPages; // 显示打印对话框
bool showPrint = await PrintManager.ShowPrintUIAsync();
} //添加打印页面的内容
private void PrintDic_AddPages(object sender, AddPagesEventArgs e)
{
//增加一个页要打印的元素
printDoc.AddPage(sp_PrintArea); //完成对打印页面的增加
printDoc.AddPagesComplete();
} private void PrintDic_Paginate(object sender, PaginateEventArgs e)
{
PrintTaskOptions opt = task.Options;
// 根据页面的方向来调整打印内容的旋转方向
switch (opt.Orientation)
{
case PrintOrientation.Default:
rottrf.Angle = 0d;
break;
case PrintOrientation.Portrait:
rottrf.Angle = 0d;
break;
case PrintOrientation.Landscape:
rottrf.Angle = 90d;
break;
} // 设置预览页面的总页数
printDoc.SetPreviewPageCount(, PreviewPageCountType.Final);
} private void OnGetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
// 设置要预览的页面
printDoc.SetPreviewPage(e.PageNumber, sp_PrintArea);
}
}

效果如下:

推荐一个UWP开发群:53078485 大家可以进来一起学习

Win10/UWP新特性系列—使用打印机的更多相关文章

  1. 【转】Win10/UWP新特性系列—Web

    Internet Explorer Internet Explorer 在Windows 10 升级为Edge模式,是一种交互性和兼容性都很强的新型浏览器,该浏览器相比以前的版本更新了超过2000个操 ...

  2. Win10/UWP新特性系列—Launcher实现应用间的通信

    UWP中,微软为Windows.System.Launcher启动器新增了很多的功能,以前只能启动App,打开指定扩展名文件,对uri协议的解析,以及当启动的应用没有安装时则会提示前往商店下载等. 如 ...

  3. Win10/UWP新特性系列-GetPublisherCacheFolder

    微软Windows Runtime App拥有很强的安全模型来防止不同App之间的数据获取和共享,也就是我们所说的"沙盒机制",每个App都运行在Windows沙盒中,App之间的 ...

  4. Win10/UWP新特性系列—电池报告

    UWP中,新增了当节电模式开启时,App能获取到通知的API,通过响应电源条件的更改,比如咨询用户是否使用黑色背景等来帮助延长电池使用时间. 通过Windows.Devices.Power命名空间中的 ...

  5. Win10/UWP新特性—Drag&Drop 拖出元素到其他App

    在以前的文章中,写过微软新特性Drag&Drop,当时可能由于处于Win10预览版,使用的VS也是预览版,只实现了从桌面拖拽文件到UWP App中,没能实现从UWP拖拽元素到Desktop A ...

  6. Win10/UWP新特性—SharedStorageAccessManager 共享文件

    首先先给大家推荐一个UWP/Win10开发者群:53078485  里面有很多大婶,还有很多学习资源,欢迎大家来一起讨论Win10开发! 在UWP开发中,微软提供了一个新的特性叫做SharedStor ...

  7. atitit。win7 win8 win9 win10 win11 新特性总结与战略规划

    atitit.win7 win8 win9 win10  win11 新特性总结与战略规划 1. win7 1 1.1. 发布时间 2009年10月22日 1 1.2. 稳定性大幅提升,很少蓝屏死机 ...

  8. Java8新特性系列-默认方法

    Java8 Interface Default and Static Methods 原文连接:Java8新特性系列-默认方法 – 微爱博客 在 Java 8 之前,接口只能有公共抽象方法. 如果不强 ...

  9. Java8新特性系列-Lambda

    转载自:Java8新特性系列-Lambda – 微爱博客 Lambda Expressions in Java 8 Lambda 表达式是 Java 8 最流行的特性.它们将函数式编程概念引入 Jav ...

随机推荐

  1. Python对整形数字进行加密和解密

    # -*- coding:utf-8 -*- __author__ = 'Ray' class Encryption: """整形数字简单的一个加密/解密算法" ...

  2. linux deepin-scrot 截图工具

    1.下载 .deb 安装包: 点击这里   (如果提示缺少依赖,去终端安装相应的依赖) 2. 设置快捷键Alt+Ctrl+A 1. 系统设置 -> 键盘设置 -> 自定义快捷键 -> ...

  3. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序更新相关数据

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第八篇:为ASP.NET MVC应用程序 ...

  4. ubuntu14.04 64bit安装teamviewer

    1.下载teamviewer,链接如下: http://downloadus2.teamviewer.com/download/version_10x/teamviewer_10.0.36281_i3 ...

  5. Ubuntu快捷键

    https://linux.cn/article-3025-1.html 超级键操作 1.超级键(Win键)–打开dash. 2.长按超级键– 启动Launcher.并快捷键列表. 3.按住超级键,再 ...

  6. createjs 的 bitmapdata类

    今天测试一个功能,在效率上出现了问题.2D舞台绘制了大量的元素,联想到AS3的 bitmapdata.darw() 功能,遗憾是createjs官方类 中没有bitmapdata类. 好在已经有大神替 ...

  7. 怎样去除ul li a标签文字下的下划线

      这个主要是text-decoration属性,颜色的话就是普通的了 <style> ul li a{ text-decoration:none; } ul li a { color: ...

  8. linux&win7双系统安装

    linux&win7双系统安装 硬盘大小分配方案 按照顺序来建立分区 /swap    4G     ==即交换分区,也是一种文件系统,它的作用是作为Linux的虚拟内存.在Windows下, ...

  9. Html5 Canvas核心技术(图形,动画,游戏开发)--基础知识

    基础知识 canvas 元素可以说是HTML5元素中最强大的一个,他真正的能力是通过canvas的context对象表现出来的.该环境对象可以从canvas元素身上获得. <body> & ...

  10. Greenplum 在Linux下的安装

    1.实验环境 1.1.硬件环境 Oracle VM VirtualBox虚拟机软件:三台Linux虚拟机:Centos 6.5:数据库:greenplum-db-4.3.9.1-build-1-rhe ...