Welcome to MSDN Blogs Sign in | Join | Help

Control Lifecycle

What happens when you create a Control? When do overrides get called and events get raised? When do styles get applied?

In response to this thread on silverlight.net, I've whipped this simple table up. There are some subtle differences between instantiating a control in XAML, and instantiating it via code that I've called out, but most of the lifecycle is the same.

Action Control instantiated in XAML Control instantiated in code
Control ctor As soon as begin tag is parsed. When you call it.
Explicit Style applied If the Style property is set in XAML, it will be applied as soon as the end tag is parsed. As soon as Style property is set.
Built-in Style (from generic.xaml) applied As soon as the end tag is parsed, after the explicit Style (if any) has been applied. Will not override explicit Style. When the control enters the tree. Will not override explicit Style.
Properties set When the attributes are parsed. When you set them.
Loaded event Posted when the element is been added to the tree. Fired before the next frame. Happens before layout. Same.
Template applied (i.e. control's visual are created from the Template) In the Measure pass of layout. The Template property will be applied if the control has no visual tree. The control starts life with no visual tree, and the visual tree will be cleared when the Template property is set. You can also call ApplyTemplate yourself. Same.
OnApplyTemplate called Whenever the Template is applied. It is not necessary to call the base OnApplyTemplate for the Template to be applied, but inherited types might be relying on it for their implementations. Same.
Visuals first available In OnApplyTemplate. Use GetTemplateChild. Same.
MeasureOverride called In the Measure pass of layout. If the Template was expanded during this Measure pass, MeasureOverride will be called after the Template has been expanded. Same.
ArrangeOverride called In the Arrange pass of layout, which occurs after the Measure pass. Same.
SizeChanged event After the Measure and Arrange passes have completed. Same.
LayoutUpdated event After SizeChanged events have fired. Same.
Published Saturday, October 11, 2008 10:28 AM by Dave Relyea

Comments

# Silverlight Cream for October 11, 2008 -- #393

Sunday, October 12, 2008 1:59 AM by Community Blogs

In this issue: Martin Mihaylov, Dave Relyea, Manish Dalal, Karl Shifflett, Terence Tsang, David Anson

# Silverlight 的控件生命周期

Sunday, October 12, 2008 5:15 AM by 木野狐(Neil Chen)

微软的DaveRelyea在blog中解释了Silverlight中控件的生命周期,以及一些可以override的基类方法发生的时机。

http://blogs.msdn.com/...

# re: Control Lifecycle

Tuesday, October 14, 2008 11:25 AM by lneir

Dave, thanks for the table.  Is this info the same for WPF?  If not, can more columns be added for WPF?

Thanks.

# re: Control Lifecycle

Tuesday, October 14, 2008 2:07 PM by Dave Relyea

Ineir,

No, there are differences with WPF. Adding a WPF column is a good idea.

# re: Control Lifecycle

Thursday, October 16, 2008 8:29 PM by cschuman

Dave, this is a great post, it's going in my favorites.

-Corey

# Loaded event timing in Silverlight

Friday, October 24, 2008 6:33 PM by Silverlight SDK

The FrameworkElement.Loaded event in Silverlight has a timing difference versus the FrameworkElement.Loaded

# Silverlight 2 basic performance test for procedural animations

Wednesday, November 05, 2008 4:34 PM by Solibulo

Silverlight 2 basic performance test for procedural animations

# Tweaking OnApplyTemplate Event Timing in Silverlight 2

Sunday, November 30, 2008 11:37 PM by Page Brooks

Tweaking OnApplyTemplate Event Timing in Silverlight 2

# re: Control Lifecycle

Thursday, December 04, 2008 6:50 PM by KCCE

Does the Created from Xaml column apply to  controls instantiated by XamlReader.Load? It seems there would be some differences since XamlReader.Load doesn't insert the control into the visual tree.

-Kendall

# Faking the Initialized Event in Silverlight

Tuesday, March 24, 2009 8:25 PM by Bryant Likes's Blog

This is another nugget of gold gleaned from the Climbing Mt Avalon workshop , although I believe this

Anonymous comments are disabled
 
Page view tracker