Flex自定义组件生命周期

时间:2010-03-02 13:12:38

标签: flex

1)preInitialize:刚刚创建组件但没有子组件存在时引发此事件。

2)initialize:在创建组件及其所有子组件之后但在计算任何维度之前引发此事件。

3)creationComplete:在创建了所有组件及其子组件之后以及在执行了所有布局计算之后调度此组件。

4)applicationComplete:在成功创建应用程序的所有组件后调度

我的问题

  1. 假设我创建了一个按钮组件,那么子组件是什么?任何人都可以深入解释组件的子组件。

  2. 任何人都可以给我看一个代码示例,其中一个组件是创建的...我的意思是从头开始的自定义组件。

2 个答案:

答案 0 :(得分:2)

我将解释组件生命周期的3个阶段:

  1. 出生
  2. 生命
  3. 死亡

    • 1.Birth:
  4.      ==>application instantiation
         ==>Create Properties, sizing
         ==>Add children
    
    • 2.Life
         ==>Validate and Invalidate Properties and sizes
         ==>Update list
            commitProperties(), measure(), and updateDisplayList().
    
          This is the way the functions communicate:
    
          invalidateProperties() –> commitProperties()‏
          invalidateSize() –> measure()‏
          invalidateDisplayList() –> updateDisplayList()‏
    
    • 3.Death
        ==>Removing Children: removeChild(), removeAllChildrens()
        ==>Garbage Collection: Collecting memory
    

    组件生命周期的概述如下......

    1. The constructor is called and initial properties are set.
    2. The preinitialize event is dispatched.
    3. The createChildren() function is called.
    4. The initialize event is dispatched.
    5. The commitProperties() function is called.
    6. The measure() function is called (if necessary).
    7. The layoutChrome() function is called (very rare and will not be covered in this post).
    8. The updateDisplayList() function is called.
    9. The creationComplete event is dispatched.
    10. The updateComplete event is dispatched.
    

答案 1 :(得分:1)

Flex SDK源代码是您的朋友。在这里查看:

http://opensource.adobe.com/svn/opensource/flex/sdk/tags/3.5.0.12683/frameworks/projects/framework/src

(您也可以通过在Flash Builder中按CTRL-SHIFT-T然后键入要打开的框架组件的名称来访问任何框架类的源代码。)

  1. 看看mx.controls.ComboBase,它是mx.controls.ComboBox的超类。它的createChildren()方法创建了几个子节点,包括边框,箭头按钮和文本输入。下拉列表(显示ComboBox.dataProvider中的项目)在ComboBox中定义并动态创建/销毁,因此不会在createChildren中创建。

  2. 所有这些类都是很好的例子,尽管有时候实现可能更清晰。 Button,CheckBox和RadioButton等简单组件是一个很好的起点。