Viewers: in countries Watching now:
Microsoft Silverlight 5 is a rich application framework for creating high-performance, cross-platform desktop and mobile applications. In this course, author Walt Ritscher demonstrates how to build a variety of applications in Silverlight, with particular focus on building compelling business applications and delivering premium video and audio content. Developers will work with the C# programming language and Visual Studio Professional, as well as Expression Blend, a tool that simplifies creation of the interactive user interfaces expected in modern-day applications.
It's hard to imagine creating a business application that doesn't work with printers. You can now capture any part of your Silverlight XAML tree and send it to the printer. Of course, you need to show the Print dialog to your users for permission before you can complete the print job. There are three major ways to print from a Silverlight application. The first is to use the browser host window. That is very easy, and I'll show you how to do that in one minute. The second is a little more complicated. If you're running an elevated trust application, you can use COM Interop to talk to Microsoft Word.
Of course Microsoft Word is a very full- featured document editor, so you can use its API to create table of contents, headers and footers, any other sophisticated content you want, and then have COM send that to the printer. And lastly, you can use the PrintDocument class inside Silverlight. Let me show you how to use the browser. I'm going to switch over to Visual Studio, and I'm in the Printing project, and I've opened the MainPage.xaml.
I'm in a regular Silverlight application ,so I'm going to press F5, which will launch my default browser; on this machine, that's Firefox. I'll then go up and choose File > Print, and then I have to choose where to output this for demonstration purposes. I could save it to Adobe PDF, which would create a file on my computer, and then launch my PDF reader. I could use Microsoft's XPS Document Writer, which will also create a file and save it on my hard drive. Or one that I like to use during demonstrations is Send To OneNote.
Let me show you how that works. I'll click OK. I'll then be asked where to place it inside OneNote. I'm going to choose General and then click OK, and there's my output results. Notice it looks like a standard browser print job. I've got things like the file name across the top of the page and the date/time and Page 1 of 1 at the bottom. So how do you take control of the printing process? On your own. In Silverlight, you use something called the PrintDocument. And at some point after you instantiate a PrintDocument, you're going to call its Print method.
That gives you access to three events: BeginPrint, PrintPage, and EndPrint. BeginPrint is where you write your code to initialize your print job. Once you're done there, Silverlight is going to call PrintPage. It's called for each page that you decide to print. It starts out by assuming you have a single page to print. Within this event you are at some point going to take the visuals that you want to show on the printed page and you're going to assign them to PrintDocument.PageVisual. Then you have to decide if you have any more pages or not.
If you do, then you set the HasMorePages = true, and then Silverlight calls PrintPage a second time. And you continue this until you set HasMorePages to false. And at that point then it fires the EndPrint event. This is where you do any cleanup, and this is also typically the place you tell your user, I've printed your document, how many pages you printed, and details like that. I have returned back to Visual Studio, and I'm going to start by looking at the code behind this Print this screen button. Double-click on the button, and here is the code.
It's pretty simple code. I'm going to instantiate a PrintDocument, I'm going to do some work--we'll talk about that in a second--and then I'm going to call the Print method on it here. Notice that you can read details off the PrintDocument, like it has a property called PrintedPageCount, which I can use to determine how many pages were printed. I have to set up an event procedure in order to actually print the data to the printer. In this case, I've decided to do it using an inline lambda instead of creating a separate method out here.
But the alternative would have been to come here and do a private void, create a method here with the correct signature, and then set up an event procedure saying call this print method that I defined down here. But I'm doing it inline here. This is the lambda expression for setting that up, and basically these are the two lines of code that are going to run during the print job. I say there's no more pages, and then I take the LayoutRoot, which is, if you look over here in MainPage, that is up here; it's this Grid. I'm going to take the entire contents of this grid and I'm going to send that XAML tree to PageVisual.
Before I show you the demo, let's go ahead and look at the other bits of code that are in here. If I want to do a multipage document, I'm going to start off the same. I create an instance of the page document, I set up my PrintPage function here inline, and then my Print title, and then let's look at what's different in here. I have a counter that I set up here and I have a constant called pageSize. This is the number of pages that I'm going to print. Now, in reality, you wouldn't do it this way. In reality what you need to do is you need to determine how big your print page is, what size paper you're using.
You need to determine how big a printing area you're going to print, like margin sizes. You need to look at your content in your Silverlight application. Let's say you've got a listbox that's got 400 rows. You need to decide how to divide that listbox up into what you consider pages. How much will fit on a page? You might have to calculate the font sizes and the length of the strings. So you're going to write some code to determine how to divide your content up into the pages. And then of course you have to divide and then send those chunks to the printer.
I have a really simplistic version here. I'm going to look at the counter, which should be 0, see that it's less than pageSize, which is 4, and then I'm going to say HasMorePages = true. Then I increment the counter, and then down here I generate a TextBlock programmatically, assign some text to it, and then assign that TextBlock to the PageVisual. Then it loops around again and I increment the counter, and I'll do this four times. Enough talk. Let's take a look at the printout. I'll press F5.
I'll click this first button and send that to OneNote. And then I'll go ahead and click OK to accept the general area, and there's my output. Notice it doesn't have the browser headers on it anymore, just my grid. And we'll do one more print. I'll come back here and click the Print pages, which is my multipage, and choose OK. And here you can see that I have Page 1. There's my text blocks, then I scroll down. There's Page 2, and Page 3 and Page 4 and Page 5.
So by adding printing support, Microsoft has made Silverlight even more appropriate for creating line of business applications.
Find answers to the most frequently asked questions about Silverlight 5 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.