2010-02-18 57 views
0
<mx:Repeater id="rep" dataProvider="{headingData.component}"> 
     <mx:HBox id="panel" label="{rep.currentItem.title}" 
width="100%" height="100%"> 
     </mx:HBox> 
    </mx:Repeater> 

protected function creationCompleteHandler():void 
      { 
       //Alert.show("init2"); 
       var components:Array = getComponentsFromXML(xml.component); 
       var i:int = 0; 
       var n:int = components.length; 
       for (i; i < n; i++) 
       { 
        panel.addChild(components[i] as DisplayObject); 
       } 
      } 

當我不把它放在中繼器標記下,組件是從XML創建的,但是如果我這樣做,那麼組件不會創建。Flex在運行時組件

任何問題。

<components type="array"> 
    <component type="mx.controls::ComboBox" title="Title One" description="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." width="230" height="220"> 
       <link linkname="Option one"/> 
       <link linkname="Option Two"/>  
    </component> 

    <component type="mx.controls::RadioButton" title="Title Two" width="230" description="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."> 
       <link linkname="Option one"/> 
       <link linkname="Option Two"/> 
    </component> 

    <component type="mx.controls::RadioButton" title="Title Three" width="230" description="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."> 
       <link linkname="Option one"/> 

    </component> 

    <component type="mx.controls::CheckBox" title="Title Four" width="230" description="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." height="220"> 
       <link linkname="Option one"/> 
       <link linkname="Option Two"/> 
       <link linkname="Option Three"/> 

    </component> 

    <component type="mx.controls::RadioButton" title="Title Five" width="230" description="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." height="220"> 
       <link linkname="Option one"/> 
       <link linkname="Option Two"/> 
       <link linkname="Option Three"/> 
    </component> 


</components> 

回答

1

好的,問題是Repeater內的id='panel'與正常情況有點不同。因爲這些組件可以重複使用,所以如果你可以設置ID,它們對於所有的組件都是一樣的。這就是爲什麼這個名字(在你的案例中是panel)實際上是一個數組(在這種情況下是一個HBox數組)。

如果你想給孩子添加到您需要選擇第一個數組元素的第一個 -

panel[0].addChild(components[i] as DisplayObject); 
+0

如何在不指定任何ID或其他參數的情況下動態地放置它 – Kevin 2010-02-19 12:13:32

+0

如果您真正解釋應該做什麼,可能會更容易幫助您。如果將panel.addChild(組件[i]替換爲DisplayObject);我粘貼的這行代碼會將這些孩子添加到第一個HBox中。 – 2010-02-19 13:01:45

0

你需要很多HBox或其中的很多組件嗎?通常,Repeater不用於嵌套的東西,就像你的例子。另外,你不應該設置你的Repeater的孩子的'id'屬性。 如果您需要一個複雜的組件,您應該從HBox繼承並將該代碼粘貼到其中。

也許,您必須將直放站換成 HBox。那麼你不應該把你的物品添加到面板中。它們應該在Repeater的dataProvider中指定。

+0

我需要一個組件在HBOX ....如果你看到我的XML ...ü可以得到它 – Kevin 2010-02-18 12:30:07

+0

HBox爲什麼在一箇中繼器?你想要更多HBox嗎? – 2010-02-18 14:02:36

+0

是的,我需要它們。 – Kevin 2010-02-19 12:13:16