当mxml描述的组件初始化它的mxml描述的属性时,Flex

时间:2011-05-28 19:08:04

标签: flex actionscript-3 properties initialization mxml

我试图覆盖一个Button类,我有一些属性,我希望直接用组件的mxml描述初始化,如:

<sl:TMyButton id="btnX" x="168" y="223" width="290" label="Button" myproperty1="10" myproperty2="101" myproperty3="4"/>

当具有mxml描述的所有属性都使用其值完全初始化时,会触发哪个函数(为了覆盖它)?

1 个答案:

答案 0 :(得分:5)

Flex组件have 4 methods in protected namespace which should be overridden to solve different tasks

  • createChildren() - 调用一次来创建和添加子组件。
  • measure()在布局过程中调用以计算组件大小。
  • updateDisplayList()具有实际组件未缩放的宽度和高度作为参数。很明显,这种方法便于儿童定位。
  • commitProperties()是我建议您覆盖的方法,以便应用不需要应用组件大小的属性值。

因此,在您的情况下,它可以是updateDisplayList()commitProperties()。我建议你使用以下代码片段:

private var myproperty1Dirty:Boolean;
private var _myproperty1:String;
public function set myproperty1(value:String):void
{
    if (_myproperty1 == value)
        return;
    _myproperty1 = value;
    myproperty1Dirty = true;
    // Postponed cumulative call of updateDisplayList() to place elements
    invalidateDisplayList();
}

private var myproperty2Dirty:Boolean;
private var _myproperty2:String;
public function set myproperty2(value:String):void
{
    if (_myproperty2 == value)
        return;
    _myproperty2 = value;
    myproperty2Dirty = true;
    // Postponed cumulative call of commitProperties() to apply property value
    invalidatePropertues();
}

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
    super.updateDisplayList(unscaledWidth, unscaledHeight);
    if (myproperty1Dirty)
    {
        // Perform children placing which depends on myproperty1 changes
        myproperty1Dirty = false;
    }
}

override protected function commitProperties():void
{
    super.commitProperties();
    if (myproperty2Dirty)
    {
        // Apply changes of myproperty2
        myproperty2Dirty = false;
    }
}

希望这有帮助!