Master pages in Silverlight - Part I

by gleblanc 5. January 2009 14:22

One of best appreciated features of ASP.NET version 2 was the master page concept. There is no reason a Silverlight application could not benefit from master pages. So, let's see how to implement master pages in Silverlight : similar capabilities though different implementation.

Our first implementation will be basic but we will improve it in forthcoming posts.

A master page is a web page (here a Silverlight page) that consists of fixed parts and one (or more) content area. The content area can be changed, for instance following an action on a menu item.

In the following Silverlight application, the master page is made of a layer at the top (with an animated text), a menu (left part) and a status bar (bottom part). In the content area, it's possible to display (at one time) one of two XAML pages. We will show how to switch content in the content area and how to pass information from one XAML content page to another one.
Everything here is kept simple since our goal is just to present the master page concept applied to Silverlight. For your information, the background image in the first content page is a painting from Alfred Sisley while the background image in the second content page is a painting from Claude Monet.



Project / source code


The master page (Page.xaml) is implemented as a 3-row grid : a canvas (with animated text) in the first row, the menu (in a vertical StackPanel) + content area the second row and the status bar in the third row : 

 <Grid x:Name="LayoutRoot" Background="AliceBlue">
  <Grid.RowDefinitions>
   <RowDefinition Height="30" />
   <RowDefinition/>
   <RowDefinition Height="20" />
  </Grid.RowDefinitions>
  .....
  <Canvas x:Name="animatedText" Grid.Row="0" >
   .....
  </Canvas>
  <Grid Grid.Row="1" >
   <Grid.ColumnDefinitions>
    <ColumnDefinition Width="100" />
    <ColumnDefinition />
   </Grid.ColumnDefinitions>
   <StackPanel Grid.Column="0">
    .....
   </StackPanel>
   <Grid x:Name="PageContainer" Grid.Column="1" />
  </Grid>
  <TextBlock x:Name="stBar" Grid.Row="2" Text="This is the status bar"
             VerticalAlignment="Center" />
 </Grid>

The two content pages are created as normal Silverlight user controls : Page1.xaml and Page2.xaml. In these two Silverlight pages, we added some common controls, to make these pages more realistic. These three XAML files (Page.xaml for the master page and Page1.xaml and Page2.xaml for the content pages) are part of the same project. There are thus included in the same XAP file.
 
In the Page.xaml.cs file, we declare two properties, corresponding to the two content pages :

 public Page1 page1 { get; set; }
 public Page2 page2 { get; set; }

These two properties are initialized in the Page constructor. Page1 is then set as the initial content page :

 public Page()
 {
  InitializeComponent();
  page1 = new Page1();
  page2 = new Page2();
  PageContainer.Children.Add(page1);
 }

To make Page2 the current content page (thus to switch content page), we write :

 SwitchToPage(page2);

with SwitchToPage which is (additionally, we display a message in the status bar) :

 public void SwitchToPage(UserControl p)
 {
  Page masterPage = Application.Current.RootVisual as Page;
  UserControl currentPage = masterPage.PageContainer.Children[0] as UserControl;
  if (currentPage != p)
  {
   masterPage.PageContainer.Children[0] = p;
   stBar.Text = "At " + DateTime.Now.ToLongTimeString() + ", switch to page named " + p.Name;
  }
 }

To give a name to a content page, we just add a Name property in the UserControl tag.

To pass information from a content page to the master page or another content page, different solutions are possible. One of them consists in storing this information as a ressource. For instance, to initialize a field named InfoPage1 somewhere in Page1.xaml.cs

 Resources.Remove("InfoPage1");
 Resources.Add("InfoPage1", "new value of InfoPage1");

To read this information from another content page :

 Page p = Application.Current.RootVisual as Page;
 string s = p.page1.Resources["InfoPage1"] as string;


To switch to Page1 directly from Page2, we would write :

 Page p = Application.Current.RootVisual as Page;
 p.SwitchToPage(p.page1);
 

In the next posts, we will improve the page switching mechanism. See you soon.
 

Tags:

Comments

1/7/2009 7:43:47 AM #

Trackback from Community Blogs

Silverlight Cream for December 06, 2009 -- #477

Community Blogs

1/7/2009 10:32:08 AM #

wow, great.

euo Republic of the Philippines

1/7/2009 10:57:09 AM #

Nice one. I've build something similar using multiple XAP files, but this is nice for smaller applications (a lot less complex).

Jonathan van de Veen Netherlands

1/7/2009 11:07:15 AM #

Great Idea.

Silverlight Travel Germany

1/7/2009 5:10:12 PM #

Pingback from silverlight-travel.com

Silverlight Travel » Master pages in Silverlight Part 1

silverlight-travel.com

1/12/2009 12:35:09 PM #

Wow, this is genious!

Why didn't I think of that before? :-P

Anna Poland

1/24/2009 3:21:29 AM #

Trackback from DotNetShoutout

Gerard Leblanc on Silverlight 2 | Master pages in Silverlight - Part I

DotNetShoutout

6/22/2009 7:29:59 PM #

Thanks a lot. That really helped me out. You don't find examples to that concept very easy Wink

Roberto Noir Germany

7/9/2009 7:38:11 AM #

I've build something similar using multiple XAP files, but this is nice for smaller applications.

club penguin cheats United States

8/18/2009 9:47:36 PM #

How to install these master pages of the silverlight. Need the instructions for the new visitors.
Regards,

Manager Employment India

8/21/2009 7:51:04 PM #

These pages are really useful and effective. I use silverlight and it is the best thing I do.
Regards,

Thesis Writing United States

8/21/2009 7:55:14 PM #

I had build such similar pages. But this note is very useful and helpful for me.
Regards,

Dissertation Help United Kingdom

8/25/2009 12:45:05 PM #

Hi,

Thanks for such good read. It helps me a lot!

I need to know one thing, I have used Menu control, using the site map I have changed menu contents.

Can same thing is possible in SL ?

Vijay Jadhav India

8/25/2009 12:53:00 PM #

Hi,

I mean Role wise menu contents will change.

Vijay Jadhav India

8/26/2009 9:53:57 AM #

I realized this functionality shortly after posting (i should play with it fully before posting!). i love this little program! thanks again

Research Papers Turkey

8/26/2009 9:55:50 AM #

I was jst had the installation and it is working very well. Really helpful and very useful.
Thanks,

Resume Writing Switzerland

8/26/2009 10:36:59 AM #

That is GREAT...I'm always looking for ways to spruce up my programming.

Acne medication United States

8/27/2009 3:46:33 AM #

I have installed it. And was using it well and is running very properly.
Thanks,

Term Papers South Africa

8/28/2009 7:17:03 PM #

I love it. And had shared the download and I have installed it too.
Regards,

Dissertation Writing Slovenia

8/28/2009 7:19:24 PM #

I love your post. Also I respect you. This is because I think how can you generate a great coding for your blog visitor.

Essay Writing Panama

8/28/2009 7:21:13 PM #

I had the update of the installing the source.
thanks,

Essay Writing Panama

8/28/2009 7:30:52 PM #

I had the update of the installing the source.
thanks,

Custom Essay Lithuania

8/28/2009 7:32:01 PM #

I can manage the things by the install products. I really feel helpful for having.

Essay Writing Jordan

8/30/2009 1:18:23 AM #

These pages are really useful and effective. I use silverlight and it is the best thing I do.
Regards,

Free games United States

8/30/2009 8:26:54 PM #

I love your post. Also I respect you. This is because I think how can you generate a great coding for your blog visitor.

Military Resume Kenya

8/30/2009 8:27:31 PM #

Might I add, your examples are great!!  I am building a website in silver light and I am using your master page concept.

Federal Resume Italy

8/30/2009 8:28:32 PM #

However, the GC is hanging on to the current Page - some Blogs are saying that I need to unregistered any events used by the User Control Current Page because GC thinks the Current Page must be active because of the registered events.  

Federal Resume Italy

8/30/2009 8:31:04 PM #

nice info! its help me a lot to finish my research regarding this topic! thank you!

Federal Resume Italy

9/1/2009 1:54:53 PM #

nice blog buddy.thanks for shareing

Laptop Bags United Kingdom

9/3/2009 10:57:05 AM #

This is really great site to watch.It have somany intresting posts
Thanku for shareing the posts.
Regards,

anti-aging United Kingdom

9/7/2009 2:00:26 PM #

I have used Menu control, using the site map I have changed menu contents.

Online University United States

9/7/2009 2:00:41 PM #

It have somany intresting posts
Thanku for shareing the posts.

Get degree United States

9/7/2009 2:01:09 PM #


I think how can you generate a great coding for your blog visitor.

Get degree United States

9/7/2009 2:01:45 PM #

great coding for your blog visitor.

Graduate diploma United States

9/7/2009 2:02:13 PM #

Thanks for sharing.

Computer Science degree United States

9/7/2009 2:02:31 PM #

Nice & Great Site.

Computer Science degree United States

9/8/2009 10:11:30 AM #

Great Info.

Master Degree United States

9/8/2009 7:02:57 PM #

Thanks for another great post on this program.  Love the updates, keep them coming.This trick lets you use the audio dongle that comes with the Touch Pro for video out without modifying anything

Exploratory Essay United States

9/8/2009 7:41:35 PM #

I agree, awesome.

CD Rates United States

9/9/2009 12:36:06 AM #

Thanks. I like the name 'Master pages'. Cool name, indeed.

Zuzan United States

9/9/2009 12:47:31 PM #

Might I add, your examples are great!!  I am building a website in silver light and I am using your master page concept.

Bulk Domains United States

9/9/2009 10:50:42 PM #

So much great info on Silverlight - thanks.

London Tour United Kingdom

9/11/2009 8:43:42 AM #

I was googling for hours! That's what I was looking for! Thanks for sharing!

essays Kazakhstan

9/12/2009 6:30:42 PM #

Yes, its really great and works excellent!

dirt bike games United States

9/13/2009 5:39:51 PM #

I keep listening to the news speak about getting free online grant applications so I have been looking around for the best site to get one.

drawing United States

9/13/2009 9:28:29 PM #

Toto ozn�men� o stavu doru.en� je generov�no automaticky.

payday online United States

9/14/2009 6:13:15 AM #

I leave a lot of comments on a lot of blogs each week - but there is one situation where I rarely leave a comment - even if the post deserves it.Good work

payday loans United States

9/14/2009 9:51:13 AM #

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.Any way Ill be subscribing to your feed and I hope you post again soon

payday loans United States

9/14/2009 6:08:16 PM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/14/2009 7:20:40 PM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/14/2009 11:53:35 PM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/15/2009 12:01:57 AM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/15/2009 1:38:23 AM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/15/2009 3:59:30 AM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/15/2009 4:43:35 AM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

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

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/15/2009 5:35:51 PM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday online United States

9/16/2009 5:38:32 AM #

thanks for shared this with us.

Buy kamagra United States

9/16/2009 5:38:54 AM #

Nice website

kamagra tablets United States

9/16/2009 11:01:25 AM #

Nice post!

free online games Turkey

9/16/2009 3:03:46 PM #

Nice posts are there in this site.Thanks for shareing the posts.
Regards,

free casino cash United Kingdom

9/16/2009 5:21:54 PM #

Nice post!
<a href="www.shop4meds.co.uk//index.php jelly</a>

kamagra jelly United States

9/16/2009 5:22:59 PM #

Nice website

cheap kamagra United States

9/16/2009 5:23:48 PM #

I havent any word to appreciate this post

cheap kamagra United States

9/16/2009 5:24:36 PM #

Pretty good post.

cheap kamagra United States

9/16/2009 5:25:45 PM #

I keep listening to the news speak about getting free online grant applications so I have been looking around for the best site to get one.

cheap kamagra United States

9/16/2009 5:26:33 PM #

I keep listening to the news speak about getting free online grant applications so I have been looking around for the best site to get one.

cheap kamagra United States

9/16/2009 5:34:15 PM #

Nice posts are there in this site.

cheap kamagra United States

9/16/2009 6:04:09 PM #

I leave a lot of comments on a lot of blogs each week

Live scores United States

9/16/2009 6:06:40 PM #

Thanks. I like the name 'Master pages'. Cool name, indeed.

basketball live score United States

9/16/2009 6:07:16 PM #

So much great info on Silverlight - thanks.

basketball live score United States

9/16/2009 7:02:22 PM #

Great Idea.

live cricket score United States

9/17/2009 5:08:48 AM #

Keep it up, your writing is always a joy to read that I even told my friends. Simply loving this!

Make Money Online United States

9/17/2009 10:33:09 AM #

I hope the first two part of this series are useful for your development on RIA Services. There is lot.

Buy Dissertation Iceland

9/17/2009 10:33:33 AM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

Buy Research Papers Finland

9/17/2009 10:34:30 AM #

I can see why my friends all run their blogs on blogengine, this stuff rocks!!

Buy Research Papers Finland

9/17/2009 11:55:56 AM #

I read this article i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

Credit Card Applications United States

9/17/2009 12:32:37 PM #

You make worthwhile points in this article. I feel like I can learn a lot from you about programming just from reading this blog.

New Jersey Dating United States

9/17/2009 6:56:09 PM #

how to get blog to show up on search engine?

how long does weed stay in your system United States

9/18/2009 1:09:21 AM #

Good post mate. Keep it up please.

free online games Turkey

9/18/2009 4:01:03 AM #

A master page is a web page (here a Silverlight page) that consists of fixed parts and one (or more) content area. The content area can be changed, for instance following an action on a menu item.

Personal Injury Lawyer United States

9/18/2009 7:06:49 PM #

I read this article i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

Research Paper United Kingdom

9/18/2009 10:36:46 PM #

I m impressed with this Great Idea.Nice sharing.

Travel Insurance Quotes United States

9/19/2009 2:15:43 AM #

These pages are really useful and effective. I use silverlight and it is the best thing I do.
Regards,

Custom Dissertation United Kingdom

9/19/2009 11:05:18 PM #

This is very useful site. very nice work. Thank you

Vintage wedding United States

9/19/2009 11:06:01 PM #

It is very nice site. Thank you

river cruise United States

9/20/2009 10:47:28 AM #

So today it is very difficult to find really good information in the internet, so in your blog I found everything I was looking for.

Buy Essay Ecuador

9/20/2009 12:58:55 PM #

I have nt any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

Best Credit Cards United States

9/21/2009 9:32:48 AM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

payday loans United States

9/21/2009 1:33:50 PM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

payday loans United States

9/21/2009 1:41:37 PM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

payday loans United States

9/21/2009 3:08:13 PM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

payday loans United States

9/21/2009 5:07:15 PM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

payday loans United States

9/21/2009 5:51:25 PM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

payday loans United States

9/22/2009 1:01:31 AM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

payday loans United States

9/22/2009 7:50:17 PM #

Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

Truss Displays United Kingdom

9/22/2009 9:43:11 PM #

Yes, the explanation is great for us newbies in programming.

1000 games United States

9/25/2009 12:23:25 AM #

I love your post. Also I respect you. This is because I think how can you generate a great coding for your blog visitor.

auto insurance quotes United States

9/25/2009 4:37:07 AM #

Hey very nice blog!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds also...

cash loans United States

9/25/2009 2:26:27 PM #

great work , good post

practice ged test United States

9/25/2009 2:28:30 PM #

nice article , thanks for sharing this information about silverlight

online high school diploma United States

9/25/2009 2:29:38 PM #

silver light rocks... keep up the good work

online high school diploma United States

9/25/2009 2:30:53 PM #

nice to explore your site

online high school diploma United States

9/25/2009 2:31:58 PM #

thanks mate, i really liked this article about silverlight

online high school diploma United States

9/27/2009 11:26:19 PM #


Nice.. found it useful.There are several posts about how to implement the master page feature in Silverlight. So the question is do we really need this master page ...

www.gleblanc.eu/.../...-in-Silverlight-Part-I.aspx

Sarong bd

9/28/2009 4:36:00 PM #

I have never read such a lovely post and I am coming back tomorrow to continue reading.

web design United Kingdom

9/28/2009 4:44:11 PM #

The text is really original and I have never seen a post like that before.  

Russian translation United States

9/30/2009 3:37:00 AM #

Super helpful, thank you!

Kid Friendly Games United States

10/1/2009 10:29:42 AM #

Silver light is the best thing I have used till now and these pages are very useful.

Jumeirah lake towers United States

10/1/2009 10:35:05 AM #

I had some confusion regarding Master page but its cleared after reading your post. Thanks for sharing.

Arabian ranches property United States

10/1/2009 10:40:50 AM #

If anyone is interested about learning more about Master pages in Silver Light then please visit:
www.silverlightshow.net/.../...in-Silverlight.aspx

Arabian ranches property United States

10/1/2009 10:45:55 AM #

Forums at silver light is the best place to learn more about Master pages, visit:
http://forums.silverlight.net/forums/t/11079.aspx

Arabian ranches property United States

10/1/2009 12:03:09 PM #

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.Any way Ill be subscribing to your feed and I hope you post again soon

Adult Toy Store United Kingdom

10/2/2009 3:24:32 PM #

Hi, I can�t understand how to add your site in my rss reader. Can you Help me, please Smile

personal loan United States

10/2/2009 3:24:40 PM #

I keep listening to the news talk about getting free online grant applications so I have been searching around for the best site to get one.

personal loan United States

10/2/2009 6:50:25 PM #

Good post. Its useful and effective.

<a href=" http://www.allsarongs.com.au/"> Sarong </a>

Sarong bd

10/3/2009 8:00:00 AM #

i will look forward for part II

resume wizard United States

10/4/2009 12:10:03 PM #

I have use internet. I have brows any site but when i used the silver light application i comfort. Better quality, silverlight on progrraming. It's my diffical choice.



<a href=" http://www.allsarongs.com/"> Sarong </a>
  

Sarong United States

10/5/2009 5:30:47 PM #

Very interesting topic will bookmark your site to check if you write more about in the future online free ads http://informationex.com

contacts United States

10/6/2009 2:22:15 PM #

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

Lawyers Latham United States

10/6/2009 7:50:28 PM #

Excellent article. Great post. Thank you

Watch House Online United States

10/6/2009 7:50:33 PM #

This is very interesting post. Thank you so much for sharing.

Watch Weeds Online United States

10/6/2009 11:59:49 PM #

Awesome stuff. I love microsoft silver light. It's great.

Free MMORPG Games United States

10/8/2009 10:34:42 AM #

this is very interesting post. Thank you so much for sharing.

auctions United States

10/8/2009 10:35:29 AM #

Good post. Its useful and effective.

dating United States

10/9/2009 12:36:32 AM #

good post its useful and effective

jer Costa Rica

10/9/2009 12:37:21 AM #

www.bodypaintcostarica.com

jer Costa Rica

10/9/2009 11:05:37 AM #

How to use windows silverlight and there is no icon that shows that it is installed please help?

toshiba laptops United States

10/9/2009 2:39:54 PM #

I would like to know more about the switching mechanism, Certainly you can explain it in a great way. See you soon.

personal contract hire United States

10/9/2009 2:40:48 PM #

if (currentPage != p)
  {
   masterPage.PageContainer.Children[0] = p;
   stBar.Text = "At " + DateTime.Now.ToLongTimeString() + ", switch to page named " + p.Name;
  }
}
Well this is really tricky, I am really impressed!

personal contract hire United States

10/9/2009 3:26:41 PM #

Credit brokers are frequently ignored when looking for a <a href="www.righttobuymortgagesuk.co.uk">right to buy mortgages</a>. A lot of people do not appreciate the savings that can be made by using a consultant. when looking for a cheap finance quote. Not only can they assist you save you cash, they can in addition offer you important information, and help you realize the complexities that loans involve.

Jayne Petters United Kingdom

10/9/2009 3:29:11 PM #

Credit brokers are frequently ignored when looking for a
[url=http://www.best-mortgage-advice-uk.co.uk]mortgage advice[/url] . A lot of people do not appreciate the savings that can be made by using a consultant. when looking for a cheap finance quote. Not only can they assist you save you cash, they can in addition offer you important information, and help you realize the complexities that loans involve.

Jayne Petters United Kingdom

10/9/2009 5:45:36 PM #


Good post.....Very nice and creative site , i really impressed about this blog.....

online poker United States

10/9/2009 10:26:37 PM #

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!
Emo

Emo United States

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

Silverlight is the best!

work at home United States

10/10/2009 9:16:55 PM #

thanks for the detailed explanation of the use of Silverlight! Smile

essay United States

10/10/2009 11:25:29 PM #

Thank you for the instructions.

where to buy electric wheelchairs United States

10/10/2009 11:27:20 PM #

I just tried this piece of code. Seems useful. Thanks!

used electric wheelchairs United States

10/11/2009 10:25:32 AM #

I am quite interesting in this topic hope you will elaborate more on it in future posts.

Dubai property listings U.A.E.

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