在 Flex 中,假设我有一个父类(super class)...类似于:
class SuperComponent extends DragStack {
private var _childReference:UIComponent;
public function SuperComponent() {
// ???
addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
}
private function onCreationComplete(e:FlexEvent):void {
//The 'this[]' technique doesn't seem to work and causes run-time errors:
//trace("Component found: " + this["myButton"]);
}
}
然后我在应用程序中使用以下派生类(仅以 MXML 模型为例):
<!-- Component ChildComponent.mxml -->
<mx:SuperComponent>
<mx:Button id="myButton" label="Press Me!" />
</mx:SuperComponent>
如何从 SuperComponent 类中验证“myButton”是否存在并引用它?我需要使用 getChildByName( ... ) 吗?
最佳答案
我不确定 DragStack 是什么类型的组件。它是否扩展了容器(Flex 3)或组(Flex4)?如果是这样,那么该组件将通过它的 lifecycle process ,并且 myButton 在执行 createChildren 方法后应该可以访问。
我相信 MXML 在幕后做了一些魔法,将按钮创建为组件的子组件。
如果 DragStack 不是容器,那么您必须告诉我们 DragStack 的默认属性是什么。 DefaultProperty 将在 class metadata 中指定.
我相信 MXML 的作用基本上是,如果没有指定其他属性,则将 XML Children 分配给 SuperComponent 类的默认属性。如果您想将其分配给不同的属性,则必须指定它,如下所示:
<mx:SuperComponent>
<mx:myProperty>
<mx:Button id="myButton" label="Press Me!" />
</mx:myProperty>
</mx:SuperComponent>
此语法通常仅在属性没有简单值的情况下使用,例如 DataGrid 的列数组。
关于apache-flex - 在哪个事件阶段,父类(super class)可以引用其派生类中潜在的现有组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6155681/