使用ActionScript更改标签文本

enl*_*loz 0 apache-flex actionscript actionscript-3

我有一个非常基本的问题.为什么这不起作用?!

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               width="1000" height="550" minWidth="960" backgroundColor="#F2F0F0">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Label id="test1" x="43" y="259" text="Label"/>

    <fx:Script>
        <![CDATA[
            test1.text = "Yay! This works...!";
        ]]>
    </fx:Script>
</s:Application>
Run Code Online (Sandbox Code Playgroud)

我收到此错误:访问未定义的属性.

谢谢!

Chr*_*sse 5

您在创建组件之前设置文本.尝试将赋值放入方法并在creationComplete上调用方法:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark"
           xmlns:mx="library://ns.adobe.com/flex/mx"
           width="1000" height="550" minWidth="960" backgroundColor="#F2F0F0" 
           creationComplete="onCreationComplete()">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label id="test1" x="43" y="259" text="Label"/>

<fx:Script>
    <![CDATA[
        public function onCreationComplete():void {
            test1.text = "Yay! This works...!";
        }
    ]]>
</fx:Script>
</s:Application>
Run Code Online (Sandbox Code Playgroud)