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

10 Laps around Silverlight 5 (Part 10 of 10)

(7 votes)
Michael Crump
>
Michael Crump
Joined Nov 12, 2010
Articles:   18
Comments:   17
More Articles
5 comments   /   posted on Dec 12, 2011
Tweet
This article is sponsored by Telerik RadControls for Silverlight. For similarly awesome content check out Telerik XAMLflix, your step-by-step guide to Telerik Silverlight and WPF controls. Get access to video tutorials, written tutorials, and tons of code!


To contact me directly please visit my blog at http://michaelcrump.net/ or through twitter at http://twitter.com/mbcrump.

This article is Part 10 of the series “10 Laps around Silverlight 5.” If you have missed any other section then please see the Roadmap below.

To refresh your memory on what Silverlight is:

Microsoft Silverlight is an application framework for writing Rich Internet Applications.

The run-time environment is available as a plug-in for most web browsers and works on a variety of operating systems including Windows, Mac and Linux.

To recap what we learned in the previous section:

  • We took a look at several new controls in Silverlight 5 including: Double and Triple click support and ComboBox Type-Ahead.
  • We then took a deeper dive into the PivotViewer control as it is more complex than the others and provided additional links.

In this article, I am going to discuss several new features that did not fit in any of the above categories.  We will discuss In-Browser HTML, PostScript and Tasks for TPL. Please review the Roadmap for the series before going any further.

The Roadmap for this Series

I’ve included the Roadmap for the series below as you may want to visit other sections as you learn Silverlight 5. I picked the following features as I thought that you may find them useful in your day-to-day work. If you want a specific topic covered then please leave it in the comments below.

1) Introduction to SL5 – This post which provides a brief history of Silverlight and relevant links.

2) Binding- Ancestor Relative Source Binding and Implicit Data Templates.

3) Graphics –XNA 3D API and Improved Graphics Stack.

4) Media - Low-Latency Sound using XNA and Remote Control and Media Command (Keys) Support.

5) Text - Text Tracking and Leading, Linked and Multi-column Text, OpenType Support, Pixel Snapped Text and TextOptions.

6) Operating System Integration - Part 1 - P/Invoke, Multiple Windows and Unrestricted File System Access in Full Trust.

7) Operating System Integration - Part 2 - Default Filename for SaveFileDialog, 64-bit browser support and Power Awareness.

8) Productivity and Performance - XAML Binding Debugging, Parser Performance Improvements and Multi-core JIT for improved start-up time.

9) Controls - Double and Triple click support, PivotViewer and ComboBox Type-Ahead.

10) Other items [This post] - In-Browser HTML, PostScript and Tasks for TPL.

In-Browser HTML

In Silverlight 4, we could use the WebBrowser control only in an “Out-of-Browser” application. This has changed in Silverlight 5 as we can now use the WebBrowser control in the browser (IE). It will however require that you make a few changes to your system. Let’s get started:

1) Update the registry – Locate the following keys:

If you running on a 32 bit machine,

HKEY_LOCAL_MACHINE\Software\Microsoft\Silverlight\

and if you are running on a 64-bit machine,

HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Silverlight\

change the UpdateConsentMode from a 0 to 1.

1

2) Sign the XAP File - Right click on the Silverlight application and go to properties. Choose Signing and check the checkbox Sign the .XAP file. Now click on the button Create Test Certificate. Enter any password that you want and hit the OK button. Now click on the More Details button.

2

Click on the Install Certificate button. Clicking on the button brings up the Certificate Import wizard. Click on the Next button and choose Place all certificates in the following store. Click on the Browse button. This will show you a Certificate Store. Choose Trusted Publisher and finish the wizard. Now repeat the same step to install this certificate in Trusted Root Certification Authorities.

3

3) Enable Out-of-Browser and Require elevated trust when running in-browser. Right click on the Silverlight application and go to properties. Place a checkmark in Enable Out-of-Browser and Require elevated trust when running in-browser.

4

Now let’s switch over to the MainPage.xaml and replace the Grid with the following code:

   1: <Grid x:Name="LayoutRoot" Background="White">
   2:        <WebBrowser x:Name="webBrowser" Source="http://michaelcrump.net/"/>
   3: </Grid>

If we run our application now (using Internet Explorer), then we will see the WebBrowser control working inside of the browser.

Note: This will not work in Chrome, Safari, others (Only Internet Explorer).

5

You can also add html pages to your project and point the source to that file as well.

PostScript Vector Printing

In Silverlight 4, we had bitmap-based printing. This meant that every time a page was going to print that Silverlight 4 sent the bitmap representing the entire page to the printer. We quickly found out that this resulted in horrible performance for larger print jobs. Now in Silverlight 5 we have PostScript Vector printing. This provides us with a much faster solution. Let’s get started building a sample application that uses this features.

Let’s switch over to the MainPage.xaml and replace the Grid with the following code:

   1: <StackPanel x:Name="LayoutRoot" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center">
   2:     <Button x:Name="basicVector" Click="basicVector_Click" />
   3:     <Button x:Name="forceVector" Click="forceVector_Click" />
   4: </StackPanel>

We can navigate over to our MainPage.xaml.cs file and add in the following code snippet for our button event handlers:

   1: private void basicVector_Click(object sender, RoutedEventArgs e)
   2: {
   3:     PrintDocument document = new PrintDocument();
   4:  
   5:     document.PrintPage += (s, ea) =>
   6:     {
   7:         StackPanel stPanel = new StackPanel();                
   8:  
   9:         for (int i = 0; i < 10; i++)
  10:         {
  11:             TextBlock tb = new TextBlock();
  12:             tb.Text = "Basic Vector mode.";
  13:  
  14:             stPanel.Children.Add(tb);
  15:         }
  16:  
  17:         ea.PageVisual = stPanel;
  18:         ea.HasMorePages = true;
  19:     };
  20:  
  21:     document.Print("Basic Vector Demo");
  22:     
  23: }
  24:  
  25: private void forceVector_Click(object sender, RoutedEventArgs e)
  26: {
  27:     PrintDocument document = new PrintDocument();
  28:  
  29:     document.PrintPage += (s, ea) =>
  30:     {
  31:         StackPanel stPanel = new StackPanel();
  32:  
  33:         for (int i = 0; i < 10; i++)
  34:         {
  35:             TextBlock tb = new TextBlock();
  36:             tb.Text = "Forced Vector mode.";
  37:  
  38:             stPanel.Children.Add(tb);
  39:         }
  40:  
  41:         ea.PageVisual = stPanel;
  42:         ea.HasMorePages = true;
  43:     };
  44:  
  45:     PrinterFallbackSettings settings = new PrinterFallbackSettings();
  46:     settings.ForceVector = true;
  47:     settings.OpacityThreshold = 0.7;
  48:  
  49:     document.Print("Forced Vector Print", settings);
  50: }

In our first button, we have a basic PostScript Vector Printing job. We create a PrintDocument (which was also in SL4), then add a PrintPage with some basic text to it. We finally print the document using Vector printing with document.Print.

In our second button, we do the same thing but we call PrinterFallbackSettings and force the print job to print in Vector mode. We can also set the Opacity value by calling OpacityThreshold.

Tasks Parallel Library (TPL)

In short, TPL is to simply asynchronous methods. Silverlight 5 does not include the full TPL but it does provide Tasks and it’s related factories. Which is exactly what you want! Let’s get started.

We are going to use a sample XML file provided by Microsoft found here. Go ahead and download it and add it to your Web Project with the name of books.xml.

Next, Let’s go ahead and navigate over to our MainPage.xaml.cs file and add in the proper namespaces to use TPL. Next, we will add in the second code snippet:

   1: using System;
   2: using System.Linq;
   3: using System.Net;
   4: using System.Threading.Tasks;
   5: using System.Windows;
   6: using System.Windows.Controls;
   7: using System.Diagnostics;
   1: public MainPage()
   2: {
   3:     InitializeComponent();
   4:     Loaded += new RoutedEventHandler(MainPage_Loaded);
   5: }
   6:  
   7: void MainPage_Loaded(object sender, RoutedEventArgs e)
   8: {
   9:     string uri = "http://localhost:15863/books.xml";
  10:  
  11:     var request = HttpWebRequest.Create(uri);
  12:  
  13:     Task.Factory.FromAsync<WebResponse>(
  14:       request.BeginGetResponse, request.EndGetResponse, null)
  15:       .ContinueWith(
  16:         task =>
  17:         {
  18:             var response = (HttpWebResponse)task.Result;
  19:             Debug.WriteLine("Content Type: " + response.ContentType);
  20:             Debug.WriteLine("Content Length: " + response.ContentLength);
  21:             Debug.WriteLine("Method: " + response.Method);
  22:             Debug.WriteLine("Status Code: " + response.StatusCode);
  23:             Debug.WriteLine("Status Description: " + response.StatusDescription);  
  24:         });
  25: }

Note: You will need to change the localhost port in the above code sample before proceeding.

Go ahead and run the application and then look at your Output window in VS2010. You should see the following information.

The thing to note here is Task.Factory.FromAsync. This creates a Tasks that represents a pair of begin and end methods that conform to the Asynchronous Programming Model Pattern.

Conclusion

At this point, we have taken a dive into all of the new features of Silverlight 5. Now that you are equipped with a solid understanding of what Silverlight 5 has to offer, you can begin to use this in your own applications. I want to thank you for reading this series and if you ever have any questions feel free to contact me on the various sources listed below. I also wanted to thank SilverlightShow.Net and Telerik for giving me the opportunity to share this information  with everyone. I hope that you enjoyed this series and if you To contact me directly please visit my blog at http://michaelcrump.net/ or through twitter at http://twitter.com/mbcrump.


Subscribe

Comments

  • hassan

    Re: 10 Laps around Silverlight 5 (Part 10 of 10)


    posted by hassan on Dec 15, 2011 21:29

    Does any buddy know what's the future of silverlight. Is  that silverlight 5 is the dead  end ??

  • Greg10

    Re: 10 Laps around Silverlight 5 (Part 10 of 10)


    posted by Greg10 on Jan 14, 2012 14:59

    Hi, I asked already and got no answer: why UpdateConsentMode and not AllowElevatedTrustAppsInBrowser?

    The former has no influence, the second works. "Enable Out-Of-Browser" setting is irrelevant in this context.

    While running on localhost neither is needed otherwise it's worth mention that when SL5 app runs in windowless mode, there's no way to get it running. Therefore probably will not work in IE on Mac either.

    Regards, Greg

  • AnnDaniel

    Re: 10 Laps around Silverlight 5 (Part 10 of 10)


    posted by AnnDaniel on Feb 04, 2012 21:34
    Thumbs up, and an awesome review for explaining silverlight.   I will be watching for other reviews in this format!
  • atif

    Re: 10 Laps around Silverlight 5 (Part 10 of 10)


    posted by atif on Mar 12, 2012 15:26

    Dear Hassan Silvelright is not dead because if you we talk about the LOB(Line of Bussiness) applications on web so we have limited technologies are there in corner and SL is one of them in Windows 8 we have new mechanism for UI which is Metro and you can easily transfer your existing skills into Metro.

    So for the web based application Silverlight is still very very powerfull.

    Now if you ask can we use HTML 5/JQuery for Large scale appplications so now and in near future these two technologies can't compete SL due to lack of tooling.

  • aasimmukhtar

    Re: 10 Laps around Silverlight 5 (Part 10 of 10)


    posted by aasimmukhtar on Feb 01, 2013 13:13

    Hi,

    I have been able to work which has been described in this article, but the application is running on localhost only, do you have any idea how to run this application using WebBrowser control with Out-Of-Browser support using the network.URL e.g. http://MACHINE_NAME/application/page.aspx

    when we use above mentioned URL it displays the same "Out-Of-Browser" error.

    Regards,

    Asim

Add Comment

Login to comment:
  *      *       

From this series