English 中文(简体)
为什么你可以在Actionscript中更改对象的id,但仍然用它的旧id引用它?
原标题:How come you can change the id of an object in Actionscript but still refer to it with its old id?

例如在Flex 4中

?xml version="1.0"?> xmlns:mx="library://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();"/>

the traces when the button is clicked twice are the id is myButton followed by the id is yourButton

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

问题回答

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

不过我承认这很奇怪。

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

请记住,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属性只是一个属性,与类的功能无关。您必须将一个新的Button实例实际分配给this.myButton才能更改它。





相关问题
Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签