(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Getting ready for Microsoft Silverlight Exam 70-506 (Part 5)

(4 votes)
Gill Cleeren
>
Gill Cleeren
Joined Apr 02, 2010
Articles:   57
Comments:   6
More Articles
1 comments   /   posted on Feb 14, 2011
Categories:   Out-of-Browser , General
SilverlightShow and Gill Cleeren start a series of materials aimed at helping you get prepared for taking Microsoft Silverlight Exam 70-506. Through this series we will try to structure the resources available on the internet, grouping them by topic covered in the exam. Any feedback would be much appreciated! Thanks! 

This article is Part 5 of the series on Microsoft Silverlight Exam.

Time flies when you’re having fun and meanwhile learning something. That certainly goes for me while I create these articles; I hope it’s the same for you when you read them. If you kept track, we have now arrived at part 5 already, so we are slowly approaching the end of the series. However, this very part is shaping up to be a large one, as you can see. It also covers a lot of interesting material in Silverlight, as we’ll soon see!

Whereas all previous parts looked at features of Silverlight itself, this part is focusing on the things that happen around Silverlight so to say. We covered data binding, controls, enhancing the UI and controls, styling and many more features that we need to know to do stuff inside Silverlight. In this fifth part, subtitled “Interacting with the host platform”, we’ll focus on the ways that Silverlight has options to do things with the machine it runs on. Among others, we’ll look at ways to store data using Isolated Storage, copy/paste data to the clipboard, allow the user to install the application on the machine as an out-of-browser application, access the HTML DOM and much more. As you can see, Silverlight is more than just building a UI: it has many ways to interact with the system on which it runs, allowing us to build even richer applications. With the help of these features, Silverlight apps come very near to “normal” applications!

For your convenience, the following list contains links to the other parts of the article series which have been finished already:

Part 5: Interacting with the host platform

Silverlight’s feature set grows with every version. While Silverlight remains a web technology, its growing set of features makes it possible to build applications that come very close to what we can build for the desktop. With Silverlight 3, we got the ability to build Silverlight applications that could be installed on the local machine. Up until then, Silverlight apps could only run in a browser. This feature is perhaps the best example of Silverlight interacting with the system it’s running on. In Silverlight 4, this mode was extended and we got Trusted Silverlight applications as well, which we’ll look at later on.

There are many more places where a Silverlight app interacts in some or the other way with the environment. Take for example the page on which it’s hosted when running in the browser. Since Silverlight 2, Silverlight has been able to interact with the page, making it possible to create or select elements or even call JavaScript on the hosting page.

Printing is another area where Silverlight has had features added. With Silverlight 4, we got the PrintDocument class, a simple way to print documents. Silverlight also has several ways to interact with files on the local system. While by default, Silverlight apps can’t read or write to the local system, a SaveFileDialog or an OpenFileDialog allow Silverlight applications to save or read a file selected by the user. In Trusted out-of-browser applications, Silverlight can even access without interactions files in the My* folders. And speaking of data, Silverlight has the ability to access the clipboard as well.

All these and the many more features we’ll explore here are ways to enrich our Silverlight applications, making it possible to build desktop-like applications. Let’s take a look at these features in some more detail!

Implement the printing API

Back in the days of Silverlight 2, printing in Silverlight was impossible. About the only thing you could do was printing from your browser, which never gives a great result: this includes a header/footer and you have no control over how the Silverlight content will be printed. With the release of Silverlight 3, no real printing solution was implemented in Silverlight yet, but a new class, the WriteableBitmap, was introduced. This API made it possible to export any control (be it the entire Silverlight control or part thereof) to a bitmap. This bitmap could then be saved and printed. While this didn’t allow for a straightforward solution to print documents, it was possible to export content and print it.

With Silverlight 4, the System.Windows.Printing namespace was introduced along with the new PrintDocument class. This class made printing directly from Silverlight possible. We can print the entire Silverlight control, a part of it or even print dynamically generated content (for example for a print preview). The printing model has support for multiple page printing (although this does require quite a lot of manual work). The output is printed as a bitmap, which in itself is no problem. On the other hand, the generated documents are quite large to send to a printer, which limits the amount of pages that you can comfortably print without hogging your network!

To learn more about printing in Silverlight, take a look at the following links:

Create out-of-browser applications

The biggest new feature with Silverlight 3 was the support to install a Silverlight application on the local machine, the so-called out-of-browser mode. Using this, a user could install the application and run it, even without opening a browser. To use OOB mode, users don’t have to install anything additionally, it’s all included in the plug-in. To support this OOB mode, a new network API was introduced as well. Using events such as NetworkAddressChanged, we can detect whether or not there’s an internet connection available from a Silverlight application. This way, we can react to going online for getting data or staying offline and using local data.

The OOB mode was deeply enhanced and extended with Silverlight 4. New features and abilities were added to it:

  • Define where the window is to be located and control window size
  • Show toast (notification) windows
  • Display web content using the WebBrowser control
  • Paint with the content of a WebBrowser control using the WebBrowserBrush

Note that while OOB applications can be installed, they still run in the context of the browser. This means that they are still running from the sandbox, just like in-browser applications!

Next to OOB mode and in-browser, a third mode was introduced: trusted Silverlight applications. As the name implies, these run with elevated permissions (the user indicates that he trusts the creator) and thus they can do quite a lot more on the local system than other apps can:

The latter is certainly the most important one, as it opens many new doors for Silverlight to interact with other components on the machine, including software such as Office, APIs such as the Speech API and hardware such as a scanner (using the COM driver).

Let’s take a look in more detail to these OOB features and options:

One last thing on COM interop: note that at this point, this is a Windows-only feature. There’s no COM interop available on a Mac (there’s no COM on the Mac platform!)

Isolated storage

By default, Silverlight applications can’t directly access the local file system. Using the OpenFileDialog, the user can give Silverlight the permission to read a file anywhere on the local file system. In this case, Silverlight gets a read-only stream to the file and therefore can’t do anything else with the file, such as deleting it. Similarly, the SaveFileDialog exists to allow saving from a Silverlight application. In both cases, we have to pass by an intermediate dialog.

On the other hand, Silverlight has access to isolated storage. In this space, it has full permissions, meaning that Silverlight can read, write or delete files or directories. The isolated storage can be seen as a small file system, specifically for the Silverlight application. The store is created per user per application. This means that when I run a specific Silverlight application, a specific store is created for me for that particular application. If someone else runs the same app on the same machine, another store is created. If I run another application, yet another store is created. This effectively means that Silverlight applications can’t access each other’s isolated storage.

An application doesn’t get unlimited space to store information. By default, an in-browser Silverlight application gets 1 MB. If it needs more, it can request so, Silverlight will display a prompt (note that more space has to be requested, should we try requesting lesser space, we’ll get an error). OOB applications get 25 MB immediately. If you run an app first OOB and then in-browser again, it will keep its 25 MB. By the way, the requested space is not really reserved on the users’ hard disk: it’s sort of a quota that the application can use.

One last thing: isolated storage is managed by the plug-in, not your browser. This means that if you go to a Silverlight application using several browsers, you’ll still be using the same isolated storage.

To learn more about isolated storage, be sure to check out the following links:

Interact with the HTML DOM

Since version 2, a Silverlight application has been capable of communicating with the page it’s hosted on. For this, the System.Windows.Browser namespace should be the place to start. Several classes and methods exist in this namespace that give us access to the browser information (headers…), cookies, query string… Also, it’s possible to access the page elements itself: we can write code that reads out the DOM, adds elements to it… Also, Silverlight can call JavaScript code on the page to execute logic embedded inside the page DOM. Vice versa also works: if configured to allow so, JavaScript code can call into Silverlight applications and therefore trigger logic inside .NET code.

With Silverlight 3, the concept of named pipes, also referred to as local messaging, was also introduced. Named pipes make it easier to write code that allows 2 Silverlight instances on the same page to communicate. This can be done manually using JavaScript but that’s a lot of work.

To learn more about how to interact with the DOM, take a look at the following links and posts:

  • Accessing cookies
  • QueryString values/ header info
    • QueryString property on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.browser.htmldocument.querystring(v=vs.95).aspx
    • http://www.silverlight.net/learn/videos/silverlight-videos/using-startup-parameters-with-silverlight/
  • Calling into JavaScript from Silverlight
    • http://timheuer.com/blog/archive/2008/03/09/calling-javascript-functions-from-silverlight-2.aspx
    • http://www.dotnetcurry.com/ShowArticle.aspx?ID=298
    • http://www.voidspace.org.uk/ironpython/silverlight/scriptable.shtml
    • http://blogs.silverlight.net/blogs/msnow/archive/2008/07/08/tip-of-the-day-15-communicating-between-javascript-amp-silverlight.aspx
  • Calling into Silverlight from JavaScript
    • http://www.voidspace.org.uk/ironpython/silverlight/scriptable.shtml
    • http://blogs.silverlight.net/blogs/msnow/archive/2008/07/08/tip-of-the-day-15-communicating-between-javascript-amp-silverlight.aspx
  • Silverlight-to-Silverlight communication
  • · Manipulating the DOM
Access the clipboard

Clipboard access allows us to copy plain text from and to the clipboard. When running as an in-browser application, Silverlight will display a prompt, asking the user if he agrees that the application accesses the clipboard.

Some more information on working with the clipboard from Silverlight can be found in the following articles:

Read from and write to the host file system

A few paragraphs back, we looked at the isolated storage as the way to allow Silverlight to manage files itself on the local system. If we want a Silverlight to gain access to a file not inside isolated storage, we have a few options:

  • Opening a file inside Silverlight can be done using the OpenFileDialog. Silverlight gets read-access to the selected file(s).
  • Saving a file, including extension, can be done using the SaveFileDialog.
  • Drag and drop a file on a Silverlight control that has the AllowDrop property set to True. This allows us to select one or more files (this doesn’t work with directories) in for example your Explorer and drag them onto a Silverlight control. When this control has the AllowDrop=True, Silverlight gets read access to the selected file(s).

If your application runs as a trusted application, it can automatically access all files within the My* folders. Trusted Silverlight applications gain full access on these directories, so they can create, update and delete files!

Take a look at the following links to learn more on this topic:

Handle alternative input methods

Apart from basic left-clicking, Silverlight has been enhanced to support other mouse functions. Scrolling was implemented partly on some controls in Silverlight 3; this was extended in Silverlight 4. Beginning from Silverlight 4, Silverlight’s default behavior for right-clicking – showing the Silverlight menu option – can be overridden to do whatever we want. Most of the time, we’ll want to display a context menu, which is what a user has come to expect when right-clicking on an item.

Silverlight isn’t limited to accepting input using just a mouse: it has support for touch input as well.

The following articles seem interesting to learn more about these topics:

  • Handling right-click
    • http://www.silverlight.net/learn/videos/all/right-click-mouse-events/
    • http://www.michaelsnow.com/2010/04/23/silverlight-tip-of-the-day-3-mouse-right-clicks/
  • Mouse wheel
    • http://weblogs.asp.net/lduveau/archive/2009/11/20/silverlight-4-mouse-wheel-support.aspx
    • http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#mousewheel
  • Multi-touch events
    • Multi-touch in Silverlight on MSDN: http://msdn.microsoft.com/en-us/library/dd894494(v=vs.95).aspx
    • http://timheuer.com/blog/archive/2009/07/30/silverlight-3-multi-touch-introduction-fundamentals-basics.aspx

Summary

Another part done and more Silverlight knowledge gained! We saw in this part how neatly Silverlight integrates with the host on which it’s running. Various options exist to interact with files, user input and host features such as the clipboard.

About Gill

Gill Cleeren is Microsoft Regional Director (www.theregion.com), Silverlight MVP (former ASP.NET MVP), INETA speaker bureau member and Silverlight Insider. He lives in Belgium where he works as .NET architect at Ordina. Passionate about .NET, he’s always playing with the newest bits. In his role as Regional Director, Gill has given many sessions, webcasts and trainings on new as well as existing technologies, such as Silverlight, ASP.NET and WPF at conferences including TechEd Berlin 2010, TechDays Belgium, DevDays NL, NDC Oslo Norway, SQL Server Saturday Switserland, Spring Conference UK, Silverlight Roadshow in Sweden… He’s also the author of many articles in various developer magazines and for SilverlightShow.net. He organizes the yearly Community Day event in Belgium.

He also leads Visug (www.visug.be), the largest .NET user group in Belgium. Gill recently published his first book: “Silverlight 4 Data and Services Cookbook” (Packt Publishing). You can find his blog at www.snowball.be.

Twitter: @gillcleeren


Subscribe

Comments

  • -_-

    RE: Getting ready for Microsoft Silverlight Exam 70-506 (Part 5)


    posted by Matt on Apr 26, 2011 20:45
    Great info! I am interested to know if anything related to API's is applicable to this exam. I wrote a tutorial on calling the Win32 API from VB.NET that might be applicable (although obviously it would need to be adapted some).

Add Comment

Login to comment:
  *      *       

From this series