Silverlight has been introduced as a new technology for more than three years, and in the early days of Silverlight, it was thought that Silverlight's capabilities could not be compared to flex, even more than Flex, in November 2009, Microsoft launched the Silverlight 4 test version, with its powerful function once again attracted a large number of developers eyeball, many once in the Flex support function, now Silverlight can be implemented, but also some new features, Flex can not be achieved. For example, the Silverlight 4 COM component interacts with features such as dragging objects from the desktop to Silverlight applications. To enable more developers to understand the Silverlight development platform, I will make a series of comparisons to Silverlight 4 and Flex 4, in the hope of helping developers learn more about Silverlight new features. In this article, you will compare the print capabilities of Silverlight 4 and Flex 4.
Silverlight 4 Printing Features
The print functionality of Silverlight 4 is controlled by the PrintDocument class, and the following steps are required to implement the printing function:
Step 1: Establish the PrintDocument object;
Step 2: Set the document name in the PrintDocument object;
Step 3: Add the event handle to the PrintPage event and add it to the Startprint and EndPrint events;
Step 4: In the PrintPage Print event, create a print window that will appear as a parent window that you want to print or control to the Print window. For example, create a StackPanel, and then add the content control you want to print in StackPanel;
Step 5: When printing, if HasMorePages is true, the description supports multiple pages for printing;
Step 6: Repeat steps 4 and 5 to achieve full Silverlight 4 printing capabilities;
It looks as if these steps are simple. Here is a routine code that prints "Hello world" after clicking the Print button.
1:using System;
2:using System.Windows;
3:using System.Windows.Controls;
4:using System.Windows.Printing;
5:
6:namespace Helloprinter
7: {
8:public Partial class Mainpage:usercontrol
9: {
10:public MainPage ()
11: {
12:initializecomponent ();
13:}
14:
15:private void Button1_Click (object sender, RoutedEventArgs e)
16: {
17:printdocument Printdoc = new PrintDocument ();
18:printdoc.documentname = "Hello world from Silverlight";
19:printdoc.printpage + = new eventhandler<printpageeventargs> (printdoc_printpage);
20:printdoc.print ();
21:}
22:
23:void Printdoc_printpage (object sender, PrintPageEventArgs e)
24: {
25:stackpanel panel = new StackPanel () {orientation = orientation.horizontal};
26:panel. Children.add (New TextBlock () {Text = "Hello",
FontFamily = new System.Windows.Media.FontFamily ("Arial"), FontSize = 12});
27:panel. Children.add (New TextBlock () {Text = "world",
FontFamily = new System.Windows.Media.FontFamily ("Arial"), FontSize = 12});
28:e.pagevisual = Panel;
29:e.hasmorepages = false;
30:}
31:}
32:}
When the button is clicked, the print event handle is invoked, and during the printing process, Silverlight automatically sets the width and height of the print window, and the Printablearea property gets the width and height of the printed page in the Print event.