如何在 MXML 视图中循环遍历子项?
我有 mxml 组件,想要遍历我在其中创建的所有子组件,而不是更深入。例如,我有这样的观点:
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:chat="org.mirapolis.virtualroom.chat.*"
height="100%" width="100%"
initialize="init()" >
<fx:Script>
<![CDATA[
import org.mirapolis.virtualroom.chat.ChatModule;
private function init() : void{
setDisplayStrategy(this);
}
private function setDisplayStrategy(element : UIComponent) : void{
for(var i : int = 0; i < element.numChildren; i++){
var child : UIComponent = element.getChildAt(i) as UIComponent;
if (child != null && child.descriptor != null){
trace(child);
setDisplayStrategy(child);
}
}
}
]]>
</fx:Script>
<mx:VBox>
<mx:Form>
<mx:FormItem label="email">
<s:TextInput id="email" />
</mx:FormItem>
<mx:FormItem>
<s:Button
label="submit" />
</mx:FormItem>
</mx:Form>
</mx:VBox>
<chat:ChatView id="id1" />
<chat:ChatView id="id2" />
</mx:VBox>
跟踪会给我这
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem10
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem10.email
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16.Button17
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2
正是我所需要的。但是,如果我向视图中添加任何 Spark 元素,child.descriptor
将给出 null
,整个事情就会崩溃。如果没有 child.descriptor
检查,此代码将深入所有组件,这是绝对不需要的。
所以我的问题是 - 是否可以获取已添加到 mxml 视图中的所有子项?
UPD: 现在效果更好了:
public static function setDisplayStrategy(element : UIComponent) : void{
trace(element);
if (element is IVisualElementContainer){
for(var i : int = 0; i < (element as IVisualElementContainer).numElements; i++){
var child : UIComponent = (element as IVisualElementContainer).getElementAt(i) as UIComponent;
if (child){
setDisplayStrategy(child);
}
}
}
}
但仍然需要太多深度:
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem10
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem10.email
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16.Button17
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.chatWrapper
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.chatWrapper.ScrollerSkin28.Group24
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.chatWrapper.ScrollerSkin28.Group24.chatArea
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.userMessage
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.send
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.chatWrapper
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.chatWrapper.ScrollerSkin105.Group101
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.chatWrapper.ScrollerSkin105.Group101.chatArea
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.userMessage
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.send
在我的示例中是否可以在 MainView7.id1 和 MainView7.id2 上停止?
I have mxml component and want to go through all its children, that I've created in it and not to go deeper. For example I have this view:
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:chat="org.mirapolis.virtualroom.chat.*"
height="100%" width="100%"
initialize="init()" >
<fx:Script>
<![CDATA[
import org.mirapolis.virtualroom.chat.ChatModule;
private function init() : void{
setDisplayStrategy(this);
}
private function setDisplayStrategy(element : UIComponent) : void{
for(var i : int = 0; i < element.numChildren; i++){
var child : UIComponent = element.getChildAt(i) as UIComponent;
if (child != null && child.descriptor != null){
trace(child);
setDisplayStrategy(child);
}
}
}
]]>
</fx:Script>
<mx:VBox>
<mx:Form>
<mx:FormItem label="email">
<s:TextInput id="email" />
</mx:FormItem>
<mx:FormItem>
<s:Button
label="submit" />
</mx:FormItem>
</mx:Form>
</mx:VBox>
<chat:ChatView id="id1" />
<chat:ChatView id="id2" />
</mx:VBox>
And trace will give me this
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem10
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem10.email
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16.Button17
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2
which is exactly what I need. But if I add any spark element to my view, child.descriptor
will give null
and the whole thing will break up. Without child.descriptor
check this code will go deeper in all components which is absolutely not needed.
So my question is - is it possible to get all children that have been added to mxml view?
UPD:
Now it works better:
public static function setDisplayStrategy(element : UIComponent) : void{
trace(element);
if (element is IVisualElementContainer){
for(var i : int = 0; i < (element as IVisualElementContainer).numElements; i++){
var child : UIComponent = (element as IVisualElementContainer).getElementAt(i) as UIComponent;
if (child){
setDisplayStrategy(child);
}
}
}
}
But still takes too many deeps:
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem10
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem10.email
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16.Button17
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.chatWrapper
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.chatWrapper.ScrollerSkin28.Group24
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.chatWrapper.ScrollerSkin28.Group24.chatArea
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.userMessage
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.send
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.chatWrapper
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.chatWrapper.ScrollerSkin105.Group101
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.chatWrapper.ScrollerSkin105.Group101.chatArea
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.userMessage
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.send
Is it possible to stop on MainView7.id1 and MainView7.id2 in my example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要循环 numElement 属性。
不过,我建议您阅读此处这篇精彩的文章
I think you need to loop over the numElement property.
However, I suggest you read this excellent post here