Flex 组件实例化生命周期

本文详细介绍Flex中组件从创建到可视化的整个生命周期过程,包括构造、配置、加入容器、初始化及布局等关键步骤,并通过ActionScript示例展示了Button组件的具体实例化流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当我们创建一个按钮、Group的时候,总想知道Flex在背后为我们做了哪些事情,尤其开发自定义组件的时候,对组件的实例化生命周期更是需要了解。Adobe 官方材料using_flex_4.pdf便详细介绍了组件实例化生命周期。

The component instantiation life cycle describes the sequence of steps that occur when you create a component object from a component class. As part of that life cycle, Flex automatically calls component methods, dispatches events, and makes the component visible.

The following example creates a Button control in ActionScript and adds it to a Group container:

// Create a Group container.

var groupContainer:Group = new Group();

// Configure the Group container.

groupContainer.x = 10; groupContainer.y = 10;

// Create a Button control.

var b:Button = new Button()

// Configure the button control.

b.label = "Submit"; ...

// Add the Button control to the Box container.

groupContainer.addElement(b);

The following steps show what occurs when you execute the code to create the Button control, and add the control to the container:

1You call the components constructor, as the following code shows:

// Create a Button control.

var b:Button = new Button()

2You configure the component by setting its properties, as the following code shows:

// Configure the button control.

b.label = "Submit";

Component setter methods might call the invalidateProperties(), invalidateSize(), invalidateSkinState(), or invalidateDisplayList() methods.

3You call the addElement() method to add the component to its parent, as the following code shows:

// Add the Button control to the Box container.

gropContainer.addElement(b);

Flex then performs the following actions:

4Sets the parent property for the component to reference its parent container.

5Computes the style settings for the component.

6Dispatches the preinitialize event on the component.

7Calls the components createChildren() method. For skinnable components, this causes a call to attachSkin(), which calls partAdded(), for all static parts defined in the skin file.

8Calls the invalidateProperties(), invalidateSize(), invalidateSkinSate(), and invalidateDisplayList() methods to trigger calls to the commitProperties(), measure(), getCurrentSkinState(), or updateDisplayList() methods during the next render event.

The only exception to this rule is that Flex does not call the measure() method when the user sets the height and width of the component.

9Dispatches the initialize event on the component. At this time, all of the components children are initialized, but the component has not been sized or processed for layout. You can use this event to perform additional processing of the component before it is laid out.

Because the initialize event is dispatched early in the component's startup sequence, make sure that none of your processing causes the component to invalidate itself. You typically perform any final processing during the creationComplete event.

10Dispatches the childAdd event on the parent container.

11Dispatches the initialize event on the parent container.

12During the next render event, Flex performs the following actions:

a Calls the components commitProperties() method. For skinnable components, the commitProperties() method calls the getCurrentSkinState() methods.

b Calls the components measure() method.

c Calls the components updateDisplayList() method.

13Flex dispatches additional render events if the commitProperties(), measure(), or updateDisplayList() methods call the invalidateProperties(), invalidateSize(), invalidateSkinSate(), or invalidateDisplayList() methods.

14After the last render event occurs, Flex performs the following actions:

a Makes the component visible by setting the visible property to true.

b Dispatches the creationComplete event on the component. The component is sized and processed for layout. This event is only dispatched once when the component is created.

c Dispatches the updateComplete event on the component. Flex dispatches additional updateComplete events whenever the layout, position, size, or other visual characteristic of the component changes and the component is updated for display.

Most of the work for configuring a component occurs when you add the component to a container by using the addElement() method. That is because until you add the component to a container, Flex cannot determine its size, set inheriting style properties, or draw it on the screen.

You can also define your application in MXML, as the following example shows:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark">

<s:Group> <s:Button label="Submit"/> </s:Group>

</s:Application>

The sequence of steps that Flex executes when creating a component in MXML are equivalent to the steps described for ActionScript.

You can remove a component from a container by using the removeElement() method.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值