`

———使用MXML标签中的id属性

阅读更多
With a few exceptions (see “MXML tag rules” on page 34), an MXML tag has an optional id property, which must be unique within the MXML file. If a tag has an
id property, you can reference the corresponding object in ActionScript.
In the following example, results from a web-service request are traced in the writeToLog function:
<?xml version="1.0"?>
<!-- mxml/UseIDProperty.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:VBox>
        <mx:TextInput id="myText" text="Hello World!" />
        <mx:Button id="mybutton" label="Get Weather" click="writeToLog();"/>
    </mx:VBox>
    <mx:Script>
        <![CDATA[
            private function writeToLog():void          {
                trace(myText.text);
            }
        ]]>
    </mx:Script>
</mx:Application>

This code causes the MXML compiler to autogenerate a public variable named myText that contains a reference to that TextInput instance. This autogenerated variable lets you access the component instance in ActionScript.
You can explicitly refer to the  control’s instance with its
id instance reference in any ActionScript class or . By referring to a component’s instance, you can modify its properties and call its methods.
Because each id value in an MXML file is unique, all objects in a file are part of the same flat namespace. You do not qualify an object by referencing its parent with dot notation, as in myVBox.myText.text.
For more information, see “Referring to Flex components” on page 42.

    除少数例外情况(见第34页“MXML标签规则”),一个MXML标签有一个可选的ID属性,此属性值在MXML文件中必须是独一无二的。如果一个标签有ID属性,你可以在ActionScript中引用相应的对象。
    在下面的例子,将使用writeToLog功能来记录一个通过网络发来的请求的执行结果:
<?xml version="1.0"?>
<!-- mxml/UseIDProperty.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:VBox>
        <mx:TextInput id="myText" text="Hello World!" />
        <mx:Button id="mybutton" label="Get Weather" click="writeToLog();"/>
    </mx:VBox>
    <mx:Script>
        <![CDATA[
            private function writeToLog():void          {
                trace(myText.text);
            }
        ]]>
    </mx:Script>
</mx:Application>

    此代码将使MXML编译器自动生成一个引用TextInput实例的公共变量myText。这种自动生成的变量,让您可以访问在ActionScript中的组件的实例。
    您可以在ActionScript类中或脚本块(script block)中明确的使用TextInput控件的Id属性来引用这它的实例,通过引用这一实例,您可以修改其属性和调用它的方法,因为每个MXML文件中的ID值是独一无二的,在一个文件中的所有对象都是同一命名空间的一部分。您通过点号来引用一个对象的父节点,例如myVBox.myText.text。
    更多有关信息,请参阅第42页“关于Flex组件”。
分享到:
评论

相关推荐

    flex文件中使用html渲染文字等.txt

    Flex提供了一种基于XML的语言——MXML(Flex标记语言)和ECMAScript的变体——ActionScript,使得开发者能够创建动态且交互性强的应用程序。 ### 二、Flex中的HTML文本渲染 在Flex中,可以通过`&lt;mx:Text&gt;`组件的`...

    flash builder4 ——简单播放器

    在MXML代码中,添加`&lt;mx:VideoDisplay&gt;`标签,定义视频的显示区域,并为其分配一个ID,如`videoDisplay`。 ```xml &lt;mx:VideoDisplay id="videoDisplay" width="100%" height="100%" /&gt; ``` 为了实现播放控制,我们...

    Flex 3 Cookbook[中文版].pdf

    - **事件处理**:通过在 MXML 文件中使用 `event="handler(event)"` 格式的属性,可以轻松地将事件绑定到 ActionScript 函数。 - **自定义组件**:使用 MXML 和 ActionScript 结合,还可以创建高度定制化的 Flex ...

    Flex3中应用CSS完全详解

    除了本地样式和外部样式表之外,Flex还允许直接在MXML标签中设置样式属性,即所谓的内联样式。这种方式提供了极大的灵活性,但不建议在大型项目中大量使用,因为它可能会导致样式难以管理和维护。 ##### 示例: ``...

    Flex4 Declarations in ActionScript

    在Flex4中,这一特性主要体现在MXML和ActionScript的结合上,使得开发者可以更直观地定义用户界面(UI)组件及其属性,而无需编写复杂的事件处理代码。这一变化大大简化了UI构建过程,使开发者能够专注于业务逻辑的...

    flex quick starts

    - **定义状态**: 使用 `&lt;mx:State&gt;` 标签定义了一个名为 "Register" 的视图状态。这里指定了 `basedOn` 属性,表示该状态基于默认的基础状态。 - **添加组件**: 在此状态下,通过 `&lt;mx:AddChild&gt;` 添加了一个确认...

    FLEX4的皮肤制作教程

    其中,`skinClass`属性是Flex SDK 4中新增的功能,用于设置当前控件所使用的皮肤。 ##### 2.3 主程序代码 ```xml xmlns:fx="http://ns.adobe.com/mflex/2009" height="254" width="576"&gt; &lt;![CDATA[ import...

Global site tag (gtag.js) - Google Analytics