如何在MXML视图中循环播放子项?

时间:2011-03-05 11:27:48

标签: actionscript-3 flash flex mxml flex-spark

我有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?

1 个答案:

答案 0 :(得分:4)

我认为你需要循环遍历numElement属性。

但是,我建议你阅读这篇优秀的帖子here