简体   繁体   English

Flex和Actionscript中的组件名称和ID,它们来自哪里?

[英]Component names and ids in Flex and Actionscript, where do they come from?

To illustrate my question. 说明我的问题。 Assume the following code snippet: 假设以下代码段:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
    <![CDATA[
        import mx.controls.Button;

        private function createButton():void
        {
            var myButton:Button = new Button();
            myButton.label = "Foo";
            this.btncontainer.addChild(myButton);
            trace ("New Button Created [" + myButton.toString() + "]"); 
        }       
    ]]>
</mx:Script>

<mx:Button label="Create Button" click="createButton()" />
<mx:VBox id="btncontainer" />

</mx:Application>

The behavior of this script should be obvious. 这个脚本的行为应该是显而易见的。 Each click of the "Create Button" button will generate a new button with a label of "Foo". 每次单击“创建按钮”按钮都会生成一个标签为“Foo”的新按钮。 What the code does and why it does it makes sense to me. 代码的作用以及代码的作用对我来说是有意义的。 My question is about the console output. 我的问题是关于控制台输出。 When I run the app in debug mode and click the "Create Button" four times I get the following in my console: 当我在调试模式下运行应用程序并单击“创建按钮”四次时,我在控制台中获得以下内容:

New Button Created [main0.btncontainer.Button15]
New Button Created [main0.btncontainer.Button19]
New Button Created [main0.btncontainer.Button23]
New Button Created [main0.btncontainer.Button27]

My question is where does the number appended to the object name come from? 我的问题是附加到对象名称的数字来自哪里? eg Button15, 19, 23, 27... etc? 例如Button15,19,23,27 ......等? Is there some sort of array in the background that holds the objects and is this an index value? 背景中是否存在某种包含对象的数组,这是一个索引值吗? Is it some sort of internal counter? 这是某种内部反击吗? Is this a pointer value of some kind? 这是某种指针值吗? In my tests, at least, why does it seem to always follow the same pattern 15, 19, 23, 27... separated by 4 each time in this case? 在我的测试中,至少,为什么它总是遵循相同的模式15,19,23,27 ......在这种情况下每次相隔4?

I understand conceptually what is happening here. 我从概念上理解这里发生了什么。 A new Button object is spawned and allocated memory. 生成一个新的Button对象并分配内存。 Each time I click "Create Button" I am creating a new instance of the Button class and adding it as a child to the VBox object. 每次单击“创建按钮”时,我都会创建一个Button类的新实例,并将其作为子项添加到VBox对象中。 I was just curious what is the meaning or significance of the numbers appended to the objects as they are created? 我只是好奇在创建对象时附加到数字的数字的含义或意义是什么?

Don't forget that since Flex is open source you can track this sort of thing down in the code. 不要忘记,因为Flex是开源的,所以你可以在代码中跟踪这类事情。

I found a function called NameUtil.displayObjectToString that seems to be responsible for creating the printable name of a Flex instance. 我发现了一个名为NameUtil.displayObjectToString的函数,它似乎负责创建Flex实例的可打印名称。 There is also NameUtil.createUniqueName which creates the name property. 还有NameUtil.createUniqueName ,它创建name属性。

Have a look at the code, but basically createUniqueName splits getQualifiedClassName to get just the class name without the package details. 看看代码,但基本上createUniqueName拆分getQualifiedClassName只获取没有包详细信息的类名。 NameUtil has a static counter that it then appends to the end of that name. NameUtil有一个静态计数器,然后将其附加到该名称的末尾。 so Button15 is the 15th FlexSprite created by your application. 所以Button15是你的应用程序创建的第15个FlexSprite。

displayObjectToString isn't too more complicated, except it follows the chain of components through the parents joining the names on a "." displayObjectToString并不是太复杂,除了它通过父节点在“。”上加入名称后的组件链。


One thing to note is a comment in UIComponent.as: 需要注意的一件事是UIComponent.as中的注释:

/**
 *  ID of the component. This value becomes the instance name of the object
 *  and should not contain any white space or special characters. Each component
 *  throughout an application should have a unique id.
 *
 *  <p>If your application is going to be tested by third party tools, give each component
 *  a meaningful id. Testing tools use ids to represent the control in their scripts and
 *  having a meaningful name can make scripts more readable. For example, set the
 *  value of a button to submit_button rather than b1 or button1.</p>
 */
public function get id():String
{
    return _id;
}

It says: "This value becomes the instance name of the object" and although that appears to be true I cannot find out where the assignment from id to name happens. 它说:“这个值将作为对象的实例名称”,虽然这似乎是真的,我无法找出从ID命名的分配情况。 It could be in the AS3 code that is generated from the MXML when it is converted during compilation. 它可能是在编译期间转换时从MXML生成的AS3代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM