【发布时间】:2010-11-22 09:53:23
【问题描述】:
假设你有一个 MyView.mxml 文件,它基本上是一个面板 几个孩子(Form、FormItems、Buttons...)。
是否可以遍历 MyView 并获取有关的所有信息 它的子项(类型、id ...)在显示之前。
如果我有这个功能,在我的 Main.mxml 中
public function iterateOverChildren(comp:Container):void {
// Get the number of descriptors.
trace("Running iterateOverChildren for " + comp.id);
if (comp != null)
{
var n:int = comp.childDescriptors.length;
for (var i:int = 0; i < n; i++) {
var c:ComponentDescriptor = comp.childDescriptors[i];
var d:Object = c.properties;
// Log ids and types of objects in the Array.
trace(c.id + " is of type " + c.type);
// Log the properties added in the MXML tag of the object.
for (var p:String in d) {
trace("Property: " + p + " : " + d[p]);
}
}
}
}
为什么这个调用不起作用?
var myV = MyView(); iterateOverChildren(myV);
仅当我添加如下语句时它才有效 addChild(myV); 在 iterateOverChildren 调用之前。 (但这不是我想要的,我想要 迭代描述而不将其添加到显示中)。
当我读到这篇文章时 http://livedocs.adobe.com/flex/3/html/help.html?content=layoutperformance_06.html
我认为“childDescriptors”方法是独立于生命周期的,它可以让我在不添加到显示的情况下内省组件。我错过了什么 ?如何在显示之前自省组件。
【问题讨论】:
-
您究竟为什么要遍历 MyView 的所有子项?根据您要查找的信息,有多种方法。
-
我在每个用户角色的后端都有每个项目(子项)的元数据。我曾经带来所有的元数据,但现在应用程序变得越来越大,并且只想从后端降低相关的元数据。在实例化之前,我想写一些动态的东西来从 MyView 中找到所有这些“id”(这是我的元数据的关键)。我知道 getChildren 结果取决于creationPolicy,所以当我读到childDescriptors 时,它看起来就像我正在寻找的东西。