Master pages in Silverlight, part IV : progressive download in the background

by gleblanc 30. January 2009 19:32

Something is killing some RIA applications : the time it sometimes takes to see the first page. So, let's see how to keep the master page as small as possible (a few KB for the master page and the initial content page) and progressively load (in the background) other content pages and all high resolution images. We will also show how to pass information between pages.  


Project / source code

As in previous posts dedicated to master pages, we create a solution that initially (as usual) contains just one project. This initial project contains the master page (Page.xaml). In this project, we add the initial content page (Page1.xaml). Images are not included as resources in the project to restreint the initial XAP file to a very reasonable size : 11 KB, that dramatically decreases the initial load time. These images (in high resolution) have to be copied in the ClientBin folder and will be loaded from the server, in the background. They will be displayed as soon as they become available on the client machine.

Page2 and Page3 content pages are created as projects in the solution : right-click on the solution (first line in the Solution Explorer window), Add New Project, Silverlight Application. Check the "Link this Silverlight control into an existing web site" checkbox but uncheck the "Add a test page" checkbox. The two projects newly created are named Proj4Page2 and Proj4Page3. Both projects contain Page.xaml and Page.xaml.cs. Feel free to change names. Prepare Page2 and Page3 as usual. After compilation, Proj4Page2.xap and Proj4Page3.xap files are created in the ClientBin folder. These files will have to be copied to the server.

How to pass information between pages ? We create another new project named Proj4Data. In the Page.xaml file, remove the Grid tag, though it is not mandatory (the goal is to keep Proj4Data.xap as small as possible). Let's assume Page2 and Page3 have to pass a list of strings to other pages. In Page.xaml.cs file of the Proj4Data project, we add :

 public static List<string> Page2Lst;
 public static List<string> Page3Lst;

In all projects but Proj4Data, we add a reference to Proj4Data : right-click on a project, Add Reference, Projects. This increases each XAP file by 4 KB, quite acceptable though it could still be possible to decrease that footprint. In our case, the master page makes the status bar accessible from any page. To do so, we also add (the control - a TextBlock- is named stBar in the master page) :

 public static TextBlock statusBar;

Now, let's link things and progressively load high resolution images and pages in the background (while the initial page is already displayed). In Page.xaml.cs of the main project (the one containing the master page and the initial content page), we add :

 int nStep = 1;
 WebClient client;
 public Page()
 {
  InitializeComponent();
  page1 = new Page1();
  PageContainer.Children.Add(page1);
  client = new WebClient();
  client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
  client.OpenReadAsync(new Uri("Clouds.jpg", UriKind.Relative));
  Proj4Data.Page.statusBar = stBar;
 }

Page1 is included in the initial XAP file. There is thus no need to explicitly load it from the server. The different loads are performed in the background. We start with the background image for the master page (the Clouds.jpg is not inserted as resource but just copied in the ClientBin folder). The last line means that Proj4Data.Page.statusBar is another way to reference the stBar control in the master page. The OpenReadCompleted function is the same as seen in the previous post :

 using System.Windows.Media.Imaging;
 using System.Windows.Resources;
 using System.Reflection;
 ..... 
 void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
 {
  BitmapImage img;
  StreamResourceInfo sri, sri4dll;
  AssemblyPart asmPart;
  Assembly asm;
  UserControl uc;
  switch (nStep)
  {
   case 1 :        // got background image for masterpage
      nStep++;
      .....        // initiate async read for Page1 background image
      break;
   case 2 :        // get background image for Page1
      .....
      nStep++;
      .....        // initiate async read for Page2
      break;
   case 3 :        // got Page3
      .....
      nStep++;
      .....        // initiate async read for Page3
      break;
   case 4:         // got Page3
      .....
      break;
  }
 }

There is no need to repeat what was explained in the previous post. Moreover, source code is provided, as usual.

See you soon for another improvement, with the dynamic load of controls that have an heavy footprint.  

Tags:

Comments

1/31/2009 5:34:37 PM #

Trackback from Community Blogs

Silverlight Cream for January 30, 2009 -- #502

Community Blogs

2/3/2009 4:23:30 PM #

Thank you. It's good articles.

vgolovchenko Russia

2/4/2009 6:30:39 PM #

Fantastic, they're very good articles.
I would need of a little help (if it's possible): How can I load an aspx page, instead a xaml page ?

Thank You !

Claudio Italy

2/5/2009 1:15:24 AM #


Hi Claudio,

You cannot replace the content area inside of my master page with an aspx page. At least, I do not see an obvious way to do it rather easily right now.

But you can replace the entire master page with an aspx page with the following instructions :

using System.Windows.Browser;
.....
HtmlPage.Window.Navigate(new Uri("http://www.gleblanc.eu/Blog/default.aspx", UriKind.Absolute));

Your question is worth a second thought. I keep it in my mind...

gerard leblanc Belgium

2/5/2009 8:02:56 PM #

Hi Gerard, thank You for the answer.
Actually i solved the problem, with an iframe element into an aspx.page (with a Silverlight control). But it isn't the best solution ...

Ops, I'm Italian, but I was born in Namur, and I lived in Belgium for 10 years ...

Claudio Italy

2/12/2009 1:32:20 PM #

Hi Gerard, Thanks very much for the nice idea you have come up with, I have downloaded the source and it also works very fine.Meanwhile I do have a doubt about the web service references in the project files.Imagine all your pages get data from the web services does it mean to add reference to the web service in all project files, if is it so then I think the .xap file size would defiantly increase. do you have any alternate idea to tackle that issue. It would be good if you can blog the same issue in your future implementations that would help to get an real world implementation of silverlight application in a modular way.

thanks

rolton India

7/24/2009 9:26:11 PM #

Tried to autotranslate you site not understand the writing any hope deutsch version?

Audemars Piguet United States

8/5/2009 6:44:34 AM #

ohh�nice post but really?/? Tong

payday loans United States

8/5/2009 6:44:37 AM #

Thank you for your help!Thank you and My best regards! Thank you and Sorry for so many questions but i really need your help.

payday loans United States

8/11/2009 3:39:48 AM #

Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?

cash loans United States

8/11/2009 3:39:51 AM #

Of course, what a great site and informative posts, I will add backlink - bookmark this site? Regards, Reader.

cash loans United States

8/20/2009 7:14:43 PM #

Thank you for your help

free business cards United Kingdom

8/20/2009 7:15:14 PM #

Thank you. It's good articles.

business card United Kingdom

8/28/2009 2:15:17 PM #

oh wow thats cool, didnt know it was possible to do that,

gives a much better user experience

thanks,

Derek

payday loans United Kingdom

9/7/2009 9:18:36 PM #

Does anyone know of any good tutorials for Silver Light for a complete novice?

London Tours United Kingdom

9/10/2009 5:10:32 PM #

Nice article thanks for posting

Samantha Peppercorn United Kingdom

9/12/2009 2:02:50 PM #

Nice information.

Multimedia degree United States

9/12/2009 2:03:10 PM #

Thanks for sharing.

distance learning university United States

9/12/2009 2:03:34 PM #

gives a much better user experience

distance learning university United States

9/12/2009 2:04:02 PM #

Great work.

distance learning university United States

9/12/2009 2:04:39 PM #

Nice post.

social work school United States

9/15/2009 6:48:02 PM #

Nice article thanks for posting

Samantha Peppercorn United Kingdom

9/16/2009 8:04:56 PM #

Nice post.

latest football scores United States

9/16/2009 8:05:36 PM #

Great work.

live score United States

9/16/2009 8:06:18 PM #

Nice article thanks for posting

wimbledon live scores United States

9/16/2009 8:07:08 PM #

gives a much better user experience

Live sport scores United States

9/16/2009 8:07:57 PM #

Thanks for sharing.

premier league live score United States

9/16/2009 8:09:22 PM #

Thank you. It's good articles.

Buy cheap kamagra United States

9/16/2009 8:10:39 PM #

Nice information.

Kamagra wholesale United States

9/16/2009 8:12:02 PM #

Thank you for your help

kamagra supplier United States

9/16/2009 8:12:48 PM #

oh wow thats cool,

buy tadalafil United States

9/16/2009 8:13:27 PM #

didnt know it was possible to do that,

buy viagra United States

9/20/2009 6:12:13 PM #

Thanks for such a detailed overview.

hair removal los angeles United States

9/21/2009 12:56:36 PM #

Your post is really great and I appreciate your straight approach.

website design United Kingdom

9/23/2009 5:28:08 PM #

VRy interesting to read it Tong Laughing

payday loans United States

9/23/2009 5:28:11 PM #

Wow! what an idea ! What a concept ! Beautiful .. Amazing � Smile

payday loans United States

9/30/2009 11:31:15 AM #

Trackback from Community Blogs

Silverlight Cream for January 30, 2009 -- #502

Tiffany Rings People's Republic of China

10/2/2009 4:16:14 PM #

You are a very smart person!

personal loan United States

10/2/2009 4:16:17 PM #

Such a usefule blog�wow !!!!

personal loan United States

10/5/2009 6:25:25 PM #

this is an excellent post.thanks for the share.

desk chairs United States

10/5/2009 6:26:21 PM #

wow...excellent.silverlight impresses me.

steam cleaners United States

10/10/2009 12:57:15 AM #

You made some good points there. I did a search on the topic and found most people will agree with your blog.

cash loans United States

10/11/2009 10:06:54 AM #

can you please specify who else would agree with him ? ;)

payday loans United States

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Gerard Leblanc is the author of several books (in french) on C++, C#, .NET and Silverlight (Eyrolles, Paris as publisher). See www.gleblanc.eu as the companion web site for these books (included sample programs).
He is Microsoft MVP for Silverlight.

MVP logo