Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5174699
  • 博文数量: 1696
  • 博客积分: 10870
  • 博客等级: 上将
  • 技术积分: 18357
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-30 15:16
文章分类
文章存档

2017年(1)

2016年(1)

2015年(1)

2013年(1)

2012年(43)

2011年(17)

2010年(828)

2009年(568)

2008年(185)

2007年(51)

分类: 嵌入式

2012-07-09 15:13:19

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.

ActionControl instantiated in XAMLControl instantiated in code
Control ctorAs soon as begin tag is parsed.When you call it.
Explicit Style appliedIf 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) appliedAs 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 setWhen the attributes are parsed.When you set them.
Loaded eventPosted 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 calledWhenever 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 availableIn OnApplyTemplate. Use GetTemplateChild.Same.
MeasureOverride calledIn 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 calledIn the Arrange pass of layout, which occurs after the Measure pass.Same.
SizeChanged eventAfter the Measure and Arrange passes have completed.Same.
LayoutUpdated eventAfter SizeChanged events have fired.Same.

References:

1.       http://blogs.msdn.com/b/devdave/archive/2008/10/11/control-lifecycle.aspx

2.      

阅读(948) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~