栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

虽然经常看到阿迪王发“看那个开发UWP的又上吊了”的图……还是忍不住重启一下这个系列。最近有用到UWP的print API,特地来写一篇给某软的这个伟大构想续一秒。

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

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

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

    
        Print
    

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

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var printHelper = new PrintHelper();
            printHelper.PreparePrintContent(this);
            await printHelper.ShowPrintUIAsync();
        }

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

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

        public PrintHelper()
        {
            printdocument = new Printdocument();
            printdocumentSource = printdocument.documentSource;
            //printdocument.Paginate += Printdocument_Paginate;
            printdocument.GetPreviewPage += Printdocument_GetPreviewPage;
            printdocument.AddPages += Printdocument_AddPages;
 
            PrintManager printMan = PrintManager.GetForCurrentView();
            printMan.PrintTaskRequested += PrintMan_PrintTaskRequested;
        }

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

printdocument.Paginate事件主要用于准备所有的预览页,该事件会在打印对话框显示时,被执行一次。如果是单页的打印该事件不处理也可以。

printdocument.GetPreviewPage事件会在显示具体的预览页时,被执行。例如供两页内容,用户前后翻页预览时,每个预览页就是由这里设置。

Sample代码里因为只有一页,所以就直接将PrintContent赋值过去了。

        private void Printdocument_GetPreviewPage(object sender, GetPreviewPageEventArgs e)
        {
            Printdocument printDoc = (Printdocument)sender;
            printDoc.SetPreviewPage(e.PageNumber, PrintContent);
        }

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

        private void Printdocument_AddPages(object sender, AddPagesEventArgs e)
        {
            Printdocument printDoc = (Printdocument)sender;
            printDoc.AddPage(PrintContent);
            printDoc.AddPagesComplete();
        }

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

        private void PrintMan_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
        {
            PrintTask printTask = null;
            printTask = args.Request.CreatePrintTask("1", sourceRequested =>
            {
                sourceRequested.SetSource(printdocumentSource);
            });
        }

在这个事件里,一般会CreatePrintTask,然后做一些打印的配置,最后指定printdocumentSource。

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

        public async Task ShowPrintUIAsync()
        {
            if (PrintManager.IsSupported())
            {
                await PrintManager.ShowPrintUIAsync();
            }
        }
 
        public virtual void PreparePrintContent(UIElement printContent)
        {
            PrintContent = printContent;
        }

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/230555.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号