[Win 10 Application Development] prints UI elements and application development ui

Source: Internet
Author: User

[Win 10 Application Development] prints UI elements and application development ui

Windows App supports the UI printing function, which is similar to the web page printing function in the browser. The advantage is that the "What you see is what you get" function directly prints the content displayed on the interface, it is much easier to create and print images.

To print data in a common App, the following types are used:

PrintManager:Located in the Windows. Graphics. Printing namespace, it is mainly responsible for displaying the Print dialog box and setting the print source. In use, call the GetForCurrentView static method to obtain a PrintManager instance, and then process its PrintTaskRequested. This event occurs when printing is required.

PrintTask:Indicates a print task. Create a print task in the PrintTaskRequested event processing of the PrintManager object.

PrintDocument:This class is critical (in the Windows. UI. Xaml. Printing namespace ). It can be used to convert the UI element into the document logic to be printed. A. process the Paginate event to calculate the printed page. After calculation, you can call PrintDocument. SetPreviewPageCount to set the total number of preview pages. B. process the GetPreviewPage event. This event occurs when a request is requested to preview a single page. during processing, you can call PrintDocument. SetPreviewPage to set a specific page to be previewed. C. When printing starts, the AddPages event will occur. At this time, PrintDocument is called. the AddPage method adds a page to the printed document logic. After all the pages to be printed are added, call the AddPagesComplete method to notify the system that the page can be submitted for printing.

 

When you are new to printing, you will think it is very complicated. In fact, when you have started an experiment, you will find that there is nothing actually. As developers of the new era, we should have the spirit of welcoming difficulties.

Let's take an example to print out the content in a RichTextBlock control on the page.

The XAML on the page is roughly as follows. la posted it directly without explanation. I believe you can understand XAML. If you cannot understand it, forget it.

<Border Padding = "30" Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid. rowDefinitions> <RowDefinition Height = "auto"/> <RowDefinition Height = "auto"/> </Grid. rowDefinitions> <StackPanel Margin = "" Orientation = "Horizontal"> <Button Content = "start printing" Click = "OnClick"/> </StackPanel> <RichTextBlock Name = "tb "Grid. row = "1" Width = "300"> <Paragraph FontSize = "36" TextAlignment = "Center" FontFamily = ""> moonlight in front of the bed, <LineBreak/> suspected to be ground frost. <LineBreak/> with a bright moon, <LineBreak/> with a low mind. </Paragraph> <Paragraph TextAlignment = "Center"> <InlineUIContainer> <Image Height = "200" Source =" http://img155.poco.cn/mypoco/myphoto/20110305/15/20110305154657_366496406.gif "/> </InlineUIContainer> </Paragraph> </RichTextBlock> ...... </Grid> </Border>

The content to be printed in this example is the guy named tb.

Enter the code file on the page and declare the following fields in the page class:

        PrintManager printmgr = PrintManager.GetForCurrentView();        PrintDocument printDic = null;        RotateTransform rottrf =null;        PrintTask task = null;

The role of the RotateTransform variable is to rotate and transform the tb to process the orientation of the printed page. If the page is horizontal, I will convert the tb to 90 degrees.

Process the PrintTaskRequested event of PrintManager, create a print task, and set the print source.

Printmgr. PrintTaskRequested + = Printmgr_PrintTaskRequested ;...... Private void Printmgr_PrintTaskRequested (PrintManager sender, PrintTaskRequestedEventArgs args) {var def = args. request. getDeferral (); // create a print task = args. request. createPrintTask ("Print test", OnPrintTaskSourceRequrested); task. completed + = Task_Completed; def. complete ();}

When calling the CreatePrintTask method to create a print task, a parameter needs to be set through a delegate to set the print source. The so-called print source,Is the document we want to print.

Private async void OnPrintTaskSourceRequrested (PrintTaskSourceRequestedArgs args) {var def = args. getDeferral (); await Dispatcher. runAsync (Windows. UI. core. coreDispatcherPriority. normal, () => {// set the print source args. setSource (printDic ?. DocumentSource) ;}); def. Complete ();}

What we want to print here is actually the PrintDocument object. The variable name seems to be wrong. The name of printDoc is reasonable. I typed it into printDic. It's okay if you know it.

The following code processes the Click Event of the button:

Private async void OnClick (object sender, RoutedEventArgs e) {if (printDic! = Null) {printDic. getPreviewPage-= OnGetPreviewPage; printDic. paginate-= PrintDic_Paginate; printDic. addPages-= PrintDic_AddPages;} this. printDic = new PrintDocument (); printDic. getPreviewPage + = OnGetPreviewPage; printDic. paginate + = PrintDic_Paginate; printDic. addPages + = PrintDic_AddPages; // display the Print dialog box bool B = await PrintManager. showPrintUIAsync ();}

After the PrintDocument is instantiated, several events of the PrintDocument must be processed.

Process the AddPages event first. This event occurs only when printing is started. In event processing, you need to add pages to the printed document. The content of each page is the UI element to be printed, for simplicity, only one page is printed for the week.

Private void PrintDic_AddPages (object sender, AddPagesEventArgs e) {// Add the printDic. AddPage (tb) page to be printed; // The report is added to printDic. AddPagesComplete ();}


Handle the Paginate event. This event occurs when the Print dialog box is opened. If you adjust the parameters in the Print dialog box, this event also occurs (for example, modifying the page direction ), the purpose is to recalculate the preview of the page.

Private void PrintDic_Paginate (object sender, PaginateEventArgs e) {PrintTaskOptions opt = task. options; // adjust the orientation switch (opt. orientation) {case PrintOrientation. default: rottrf. angle = 0d; break; case PrintOrientation. portrait: rottrf. angle = 0d; break; case PrintOrientation. landscape: rottrf. angle = 90d; break;} // sets the total number of pages on the preview page, printDic. setPreviewPageCount (1, PreviewPageCountType. final );}

 

The following code adds a preview of a specific page.

Private void OnGetPreviewPage (object sender, GetPreviewPageEventArgs e) {// sets the printDic. SetPreviewPage (e. PageNumber, this. tb) of the page to be previewed );}


Note,Page Preview and actual printing are two different things. Therefore, SetPreviewPage is only used to set the UI elements to be previewed, and the actual printing is to add pages through the AddPage method in the AddPages event..

 

When the example is completed, you may have another problem. I don't have a printer. What should I do? It's okay. Lazy is poor and there is no printer, but don't forget that the system has these features:

 

The inventory file is saved in the "document" directory.

 

Run the application and click "start printing.

 

The Print dialog box is displayed.

 

Click print. After printing is complete, the system will send you a Toast notification. The output file is displayed.

 

Okay, isn't it a little tall?

Sample Code download: http://files.cnblogs.com/files/tcjiaan/printSample.zip

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.