Master pages in Silverlight - Part II

by gleblanc 9. January 2009 19:29

In Part II of this serie dedicated to master pages in Silverlight, we will improve the previous program by adding an animation that is executed at page switching time.


Project / source code

First, we are dynamically adding an animation to each content page (here Page1 and Page2) : 

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

  // animation for Page1 (Scale animation on X)
  ScaleTransform scaleTrans = new ScaleTransform();
  scaleTrans.SetValue(NameProperty, "scale");
  page1.RenderTransform = scaleTrans;
  page1.RenderTransformOrigin = new Point(0.5, 0.5);
  Storyboard stb = new Storyboard();
  stb.Duration = new Duration(TimeSpan.FromSeconds(1));
  DoubleAnimation daX = new DoubleAnimation {From=0.1, To=1 };
  Storyboard.SetTargetName(daX, "scale");
  Storyboard.SetTargetProperty(daX, new PropertyPath("ScaleX"));
  stb.Children.Add(daX);
  page1.Resources.Add("AnimPage", stb);

  // animation for Page2 (Scale animation on Y)
  scaleTrans = new ScaleTransform();
  scaleTrans.SetValue(NameProperty, "scale");
  page2.RenderTransform = scaleTrans;
  page2.RenderTransformOrigin = new Point(0.5, 0.5);
  stb = new Storyboard();
  stb.Duration = new Duration(TimeSpan.FromSeconds(1));
  DoubleAnimation daY = new DoubleAnimation {From=0.1, To=1 };
  Storyboard.SetTargetName(daY, "scale");
  Storyboard.SetTargetProperty(daY, new PropertyPath("ScaleY"));
  stb.Children.Add(daY);
  page2.Resources.Add("AnimPage", stb);
 }

Each Storyboard for animation is named AnimPage and is added as resource in the page. An animation takes some time, here one second. To disable page switching while an animation is active, we declare a boolean :

 bool bRunningAnim;

Our SwitchToPage function becomes :

 public void SwitchToPage(UserControl p)
 {
  if (bRunningAnim) return;   // animation in progress
 
  Page masterPage = Application.Current.RootVisual as Page;
  UserControl currentPage = masterPage.PageContainer.Children[0] as UserControl;
  if (currentPage != p)
  {
   masterPage.PageContainer.Children.Add(p);
   Storyboard stb = p.Resources["AnimPage"] as Storyboard;
   stb.Completed += new EventHandler(stb_Completed);
   stb.Begin();
   bRunningAnim = true;
  }
 }

The new page is first added to the children of the content area grid cell (PageContainer, see previous post). Two pages are now displayed (more precisely, the second one will soon be displayed). We retrieve the AnimPage animation and launch it.

In the Completed function, we remove the old page from the content area and mark the animation as terminated :

 void stb_Completed(object sender, EventArgs e)
 {
  if (!bRunningAnim) return;
 
  Page masterPage = Application.Current.RootVisual as Page;
  masterPage.PageContainer.Children.RemoveAt(0);
  bRunningAnim = false;
 }

See you soon for a next improvement.

Tags:

Comments

1/11/2009 12:03:02 PM #

Trackback from Community Blogs

Silverlight Cream for January 10, 2009 -- #481

Community Blogs

1/20/2009 8:32:11 AM #

Hellow, nice blog, tnx for the information,.....

Busby SEO Test United States

1/24/2009 3:22:59 AM #

Trackback from DotNetShoutout

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

DotNetShoutout

2/14/2009 1:01:02 PM #

ill never try a Microsoft Silverlight, but i think this is OK to you.
thanks for the information next time i try SilverLight.
thanks

Microsoft Registry Cleaner Republic of the Philippines

3/12/2009 3:15:43 PM #

another progress to silverlight

Perfect Home Design United States

3/13/2009 2:35:12 PM #

thanks for the codes! thanks thanks thanks (^_^)

global gifting system team United States

3/16/2009 1:53:04 PM #

Thanks for the post, this is a great job.

giving4prosperity United States

3/17/2009 7:18:44 AM #

nice post... i like this informative information of silver light.... more post please... i enjoy reading this...Thanks dude...

Secured Homeowner Loan United States

4/14/2009 1:55:07 PM #

im not really understand, but still try...looks to complicated.
Thanks for the info...

comforter down United States

4/15/2009 12:57:44 PM #

Thanks for the post, love reading this one.

goodpeoplegives United States

4/15/2009 1:07:43 PM #

Keep up the good job.

cash4trends United States

4/15/2009 1:29:20 PM #

Continue this good job.

billrainier United States

5/23/2009 5:32:19 PM #

Good post, but have you thought about Master pages in Silver light - Part II before?

Loan Modification Service India

5/23/2009 5:33:01 PM #

I was just thinking about Master pages in Silverlight - Part II and you've really helped out. Thanks!

Loan Modification Expert India

6/21/2009 12:34:26 AM #

thanks for the great article about Master pages in Silver light Part II, this really nice blog

tukang nggame United States

6/25/2009 2:27:13 PM #

This is great thanks for the share.

http://www.24hpayday.com

bad credit payday loans United States

7/9/2009 7:36:23 AM #

I'll never try a Microsoft Silverlight, but i think this is OK to you.
thanks for the information next time i try SilverLight.

club penguin cheats United States

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

I don't like your template but your posts are quite good so I will check back!

Adee Kaye United States

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

Please, can you PM me and tell me few more detailles about this, I am really fan of your web log...

payday loans United States

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

You are a very smart person!

payday loans United States

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

That�s Too nice, when it comes in india hope it can make a Rocking place for youngster.. hope that come true.

cash loans United States

8/11/2009 3:40:01 AM #

I think your blog need a new wordpress template. Downalod it from http://templates.wordpressguru.in, The site has nice and unique wordpress templates.

cash loans United States

8/13/2009 11:59:29 AM #

Very nice post!Thanks and keep new updates...

SEO Reseller United States

8/14/2009 10:28:34 AM #

I have been reading a lot on here and have picked up some great ideas.

Oceanside Thrift Clothing United States

8/14/2009 10:28:39 AM #

I think you made a great point. One that I have been thinking about as well.

Goose Hunting Canada United States

8/15/2009 7:13:28 AM #

thanks for sharing the great tips. Love it very much

Awek comel Thailand

8/15/2009 7:14:01 AM #

Interesting insight. Great thought and well said.

Awek seksi Thailand

8/17/2009 1:11:58 PM #

The content on this site is unique. A good work done related to tutorials and must be carried on.

Internet marketing United Kingdom

8/17/2009 5:41:40 PM #

Thanks for the code, they are just life needed.

los angeles web design United States

8/18/2009 7:14:08 PM #

I’m really impressed with your article, such great information you mentioned here, thanks for your sharing and waiting to see your future posts.

Employment Opportunities Thailand

8/20/2009 5:16:14 AM #

That was the best article I have read in a long time!

Buying Selling Home United States

8/20/2009 5:16:18 AM #

Great insights. I loved to read your article. You must be putting a lot of time into your blog!

My Website On Google United States

8/24/2009 1:37:49 PM #

Interesting insight...I’m really impressed with your article...Love it very much.

tetris Syria

8/25/2009 12:59:03 PM #

Gr8!

Can I used Menu control just like ASP:Menu instead of Hyper link ?

Vijay Jadhav India

9/8/2009 7:42:09 PM #

This picture is amazing.

Banks United States

9/9/2009 12:12:35 AM #

Nice code. Thanks for that.

Bookie United States

9/12/2009 12:22:28 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 online United States

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

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

cash loans United States

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

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

payday online United States

9/14/2009 6:13:16 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 8:47:24 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 9:50:26 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 11:59: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/15/2009 12:36:15 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:03:39 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 10:38:42 PM #

Please, can you PM me and tell me few more detailles about this, I am really fan of your web log.

Emo

Emo United States

9/16/2009 12:56:01 AM #

Thanks for providing such useful information. I really appreciate your professional approach.

online drug store United States

9/17/2009 12:07:19 AM #

i like this informative information of silver light.... more post please... i enjoy reading this...Thanks dude...

Free online games United States

9/17/2009 10:25:00 AM #

What is Silverlight? How to use it?

Life insurance United Kingdom

9/18/2009 5:57:44 PM #

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

агентство переводов United Kingdom

9/21/2009 1:39:33 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 2:12: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/22/2009 1:00:49 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/23/2009 5:28:27 PM #

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

payday loans United States

9/23/2009 5:28:30 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.

payday loans United States

9/25/2009 12:22:06 AM #

Thanks for providing such useful information. I really appreciate your professional approach.

auto insurance quotes United States

9/25/2009 11:27:37 AM #

Wonderful article, thanks for putting this together! This is obviously one great post. Thanks for the valuable information and insights you have so provided here

Resume Writing Service United States

9/25/2009 11:28:12 AM #

Really interesting articles.I enjoyed reading it. I need to read more on this topic..Thanks for sharing a nice info...

Buy Essay Paper United States

9/27/2009 3:06:04 PM #

Very interesting post..Iam very much enjoyed by reading your site..this article has provided a useful info..Thanks for the info given..

whiten teeth South Africa

9/29/2009 3:44:09 PM #

Very interesting post..Iam very much enjoyed by reading your site.

outdoor fireplace United States

9/30/2009 11:28:44 AM #

Very interesting post..Iam very much enjoyed by reading your site.

Tiffany Rings People's Republic of China

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

words cant say how can i thank you for this. by the thanks for the information you have provided for us it really a great help.

resume wizard United States

10/5/2009 7:16:18 PM #

I am very glad to see such information which I was searching for a long time.This made very glad..This site has given us an useful information..

anti aging products Spain

10/5/2009 7:23:20 PM #

Very interesting site..Useful information shared..Iam very happy to read this article..thanks for giving us nice info..Keep up the work..

best teeth whitening products Peru

10/5/2009 7:32:07 PM #

Useful information shared..Iam very happy to read this article..thanks for giving us nice info.Fantastic walk-through. I appreciate this post.

green tea for weight loss Mexico

10/5/2009 9:34:54 PM #

I enjoyed reading it. I need to read more on this topic..Thanks for sharing a nice info..

free music download websites Jordan

10/5/2009 10:51:48 PM #

You have provided answers for almost all the questions I had in my head.I need to read more on this topic..Thanks for sharing a nice info...

work at home jobs Germany

10/7/2009 10:15:46 AM #

Fantastic walk-through. I appreciate this post.There is obviously a lot to know about this. I think you made some good points in Feature also

wine gifts Croatia

10/8/2009 12:39:19 PM #

This is good site to watch.i think this is OK to you.
thanks for the information next time i try SilverLight. Thanks for sharing.
Regards,

losing weight during pregnancy United States

10/9/2009 4:41:39 PM #

Each Storyboard for animation is named AnimPage and is added as resource in the page. An animation takes some time, here one second. To disable page switching while an animation is active, we declare a boolean..

play free online games United States

10/10/2009 1:27:14 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:27:55 AM #

Interesting blog, just bookmarked it for later reference

Property Software 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