天天看點

flex creationComplete事件的一些思考

creationComplete恐怕是flex用的最多的事件了,不管啥,application标簽後面接個creationcomplete事件處理函數,做為程式的起點。這種做法是沒錯的,可是有些時候它也不是都管用。先看看來自devguide_flex3的論述:

  The preinitialize event occurs too early in the component life cycle for most initialization activities. It is useful, however, in the rare situations where you must set the properties on a parent before the children are created.

To configure a component before Flex has determined its visual appearance, use the initialize event. For example, use this for setting properties that affect its appearance, height, or width.

Use the creationComplete event for actions that rely on accurate values for the component’s size or position when the component is created. If you use this event to perform an action that changes the visual appearance of the component, Flex must recalculate its layout, which adds unnecessary processing overhead to your application.

Use the updateComplete event for actions that must be performed each time a component’s characteristics change, not just when the component is created.

這是flex components initialization lifecycle裡論述的關于元件初始化的過程(個人認為這是flex的超級重點)。其它事件不看了。creationcomplete事件發生在某個元件(以及它的子元件)完全建立的時候,包括樣式,大小,布局完成。也就是講,是你能看到UI的時候。它隐含了一些意思在裡面(個人經驗之談),很可能在實際操作過程中被忽略掉。

必須可見 。我在這點上吃了幾次虧。creationcomplete的時機是布局已經完成的時刻,那如果某些子元件不在布局之内呢,比如tag navigator, viewstack(其它的我沒試過,不好講)。它的還沒被選中的子元件就沒有初始化,通路它就報錯了。

隻發生一次 。就是建立的時候,如果以後布局有變化,發出的是updatecomplete 事件。

每個元件都有 。除了application,還有它的子元件,子元件的子元件… 。地球人都有呀。這一點可以做為第一點的解決方案,就是在子元件裡各自建立creationcomplete的處理函數,這樣對資料子產品化也有好處。

文章來自: 閃無憂(www.5uflash.com) 詳文參考:http://www.5uflash.com/Flex-AIR/Flexziliao/4424.html