Actionscript 3 如何在Flex中运行时获取BorderContainer的id
在运行时尝试获取BorderContainer的id时出错。我尝试使用getStyle,但也失败了Actionscript 3 如何在Flex中运行时获取BorderContainer的id,actionscript-3,apache-flex,flex4.5,mxml,Actionscript 3,Apache Flex,Flex4.5,Mxml,在运行时尝试获取BorderContainer的id时出错。我尝试使用getStyle,但也失败了 <s:Panel id="colorPanel" title="Dem display color" width="500" height="500"> <s:layout> <s:BasicLayout/> </s:layout> <s:Label id="
<s:Panel id="colorPanel"
title="Dem display color"
width="500" height="500">
<s:layout>
<s:BasicLayout/>
</s:layout>
<s:Label id="label" y="4" horizontalCenter="0"/>
<s:BorderContainer id="Box1" x="70" y="70" height="50" width="50" backgroundColor="#0000ff">
</s:BorderContainer>
<s:BorderContainer id="Box2" x="90" y="90" height="51" width="50" backgroundColor="#00ff00">
</s:BorderContainer>
<s:BorderContainer id="Box3" x="50" y="50" height="52" width="50" backgroundColor="#ff0000">
</s:BorderContainer>
<s:Button label="Click" click="
colorPanel.setElementIndex(colorPanel.getElementAt(0),3);
label.text = ""+colorPanel.getElementAt(0).id ;
">
</s:Button>
</s:Panel>
我发现的解决方案似乎过于僵化了。当我将返回的元素转换为BorderContainer时,我就能够得到Id的值。因此,我们必须接受它,直到Adobe降低了编译器对我们的期望
label.text = ""+ BorderContainer(colorPanel.getElementAt(0)).id ;
不必强制转换到边界容器,而是可以安全地强制转换到UIComponent。在您的示例中,当返回的元素是Label而不是BorderContainer时,它会崩溃。您可以执行以下操作:
<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script><![CDATA[
import mx.core.UIComponent;
]]></fx:Script>
<s:Panel id="colorPanel"
title="Dem display color"
width="500" height="500">
<s:layout>
<s:BasicLayout/>
</s:layout>
<s:Label id="label" text="Red" y="4" horizontalCenter="0"/>
<s:BorderContainer id="Blue" x="70" y="70" height="50" width="50" backgroundColor="#0000ff">
</s:BorderContainer>
<s:BorderContainer id="Green" x="90" y="90" height="51" width="50" backgroundColor="#00ff00">
</s:BorderContainer>
<s:BorderContainer id="Red" x="50" y="50" height="52" width="50" backgroundColor="#ff0000">
</s:BorderContainer>
<s:Button label="Click" click="{
colorPanel.setElementIndex(colorPanel.getElementAt(1),3);
label.text = UIComponent(colorPanel.getElementAt(3)).id;}"/>
</s:Panel>
</s:Application>
我不明白你为什么需要它,但是你可以用
名称来代替id
。