【问题标题】:How come you can change the id of an object in Actionscript but still refer to it with its old id?为什么你可以在 Actionscript 中更改一个对象的 id,但仍然使用它的旧 id 来引用它?
【发布时间】:2010-10-16 23:02:09
【问题描述】:

例如在 Flex 4 中

?xml 版本="1.0"?> xmlns:mx="库://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">

<s:layout> 
    <s:VerticalLayout/> 
</s:layout>

<fx:Script>
    <![CDATA[
        private function setLabel():void {

            trace ("the id is "+myButton.id);
            myButton.id = "yourButton";

        }

    ]]>
</fx:Script>

<s:Button id="myButton" label="Click Me" click="setLabel();"/>

点击按钮两次时的痕迹是 'id 是 myButton' 后跟 'id 是 yourButton'

不仅仅是一个空闲的查询。我希望在用它们填充主应用程序时更改自定义组件的 id

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    我假设在 mxml 中设置时的 id 是变量名,它也设置了内部 id (myButton.id = "myButton") 因此你可以将 myButton.id 更改为 "yourButton" 因为 id和变量名是不同的属性。

    虽然我承认很奇怪。

    如果您想在填充主应用程序时创建自定义组件,我会寻找一种不同于将它们全部放在 mxml 中的方法。也许在 actionscript 中创建组件并在 mxml 中设置它们是最好的? (例如,您的主类是 mxml 应用程序,然后您有一个类,它使用所有自定义命名组件来完成创建视图的繁重工作)

    【讨论】:

      【解决方案2】:

      请记住,MXML 由 mxmlc 解析为 ActionScript。 mxmlc 在编译时使用 mxml 标记的 ID 属性映射到公共类成员。更改 ID 字段的运行时值不会改变类结构。

      例子:

      <MyComponent> <Button id="myButton" /> &lt/MyComponent>

      编译后,mxmlc 大致转换为:

      
      package {
        class MyComponent {
      
           [Bindable]
           public var myButton:Button;
      
           // Other junk for class init, etc would show here...
      
        }
      }
      

      然后被编译为 SWF 字节码。此时,ID 属性只是一个属性,与类的功能无关。您实际上必须为 this.myButton 分配一个新的 Button 实例才能更改它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-25
        • 2016-06-28
        • 1970-01-01
        • 2015-09-10
        • 2011-07-04
        • 2021-12-15
        • 2021-08-06
        • 1970-01-01
        相关资源
        最近更新 更多