———使用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组件”。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值