开发者

How does Silverlight Loaded event work?

I posted this on Silverlight.net (here). I guess they are as stumped about how load works as everyone else because I didn't get a response.

The MSDN documentation found says :

You can handle LayoutUpdated instead of Loaded. LayoutUpdated is the last "object lifetime" event in the sequence of enabling a control in Silverlight UI. The main limitation of LayoutUpdated is that the initialization is possibly not the only time that LayoutUpdated is raised.

(Question 1) The inverse of the above statement implies that the Loaded event can be relied on to be raised only once.  Is that correct??

Also the MSDN document referenced above says (with respect the Loaded Event):

Occurs when a FrameworkElement has been constructed and added to the object tree. (Inherited from FrameworkElement.)

The above statement is repeated and emphisized in the document referenced here which appears to try to reconcile the issue.

However the detail documentation for the Loaded Event says:

In Silverlight, the Loaded event is not guaranteed to occur after the template is applied. This might be an issue for you if you are using the Loaded event for a relatively common control scenario: you want to examine the visual tree...

(Question 2) This is very confusing.  (The above two statements are contradictory) OR (it is possible for a FrameworkElement to be constructed and added to the object tree AND the temlate has not been applied).  The latter seems unlikely since the second msdn statement references the visual tree.  Please clarify whic开发者_开发知识库h of these statements is correct.  (Question 3) Also, does "not guranteed to occur after" mean that it may occur before, or it just wont occur at all?


The Loaded event will be raised only once, unless you remove the object from the object tree and re-add it later, at which point everything will happen all over again.

I can confirm that the Loaded event can happen before the template is applied. I do all my template and visual tree examination operations, including any last-minute initialization, in OnApplyTemplate() just for this reason.

"Not guaranteed to occur after" refers to the order of operations, yes. Loaded will always happen after an object is added to the object tree.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜