Flex4之ActionScript类编程【面向对象】

1.首先看一个节省代码的一般写法

  mxml组件:MyGruop.mxml

 

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
   xmlns:s="library://ns.adobe.com/flex/spark" 
   xmlns:mx="library://ns.adobe.com/flex/mx" width="250" height="108">
 <fx:Script>
  <![CDATA[
   //要加上public修饰外边文件方可使用到
   [Bindable]
   public var fileName:String;
   [Bindable]
   public var age:String;
  ]]>
 </fx:Script>
 
 <mx:Form x="21" y="10" width="220" height="88">
  <mx:FormItem label="姓名:">
   <s:TextInput text="{fileName}" />
  </mx:FormItem>
  <mx:FormItem label="年龄:">
   <s:TextInput text="{age}"/>
  </mx:FormItem>
 </mx:Form>
</s:Group>

 

主应用程序的main.mxml中只需调用这样的代码

   <components:MyGroup fileName="梁静茹" age="88" />  
   <components:MyGroup fileName="周杰伦" age="31" />  

 

2.由于上边调用时值直接写属性有点硬编码,所以想到用类似JAVA程序的对象来处理效果会更好

   在Flex4中那么就会出现一个ActionScript类,以.as结尾的文件

  MyActionScript.as

 

package components
{
	public class MyActionScript
	{
		[Bindable]
		public var fileName:String;
		[Bindable]
		public var age:String;
		public function MyActionScript(fileName:String,age:String)
		{
			this.fileName=fileName;
			this.age=age;
		}
	}
}

 

MyGroup.mxml中代码不变,main.mxml中这么使用

   先在<fx:Script>中实例化对象

  [Bindable]   
   private var mac:MyActionScript=new MyActionScript("蔡依林","30");
    [Bindable]
   private var mac1:MyActionScript=new MyActionScript("王力宏","31");

   然后具体使用代码为

  <mx:FormItem>
   <components:MyGroup fileName="{mac.fileName}" age="{mac.age}" />  
   <components:MyGroup fileName="{mac1.fileName}" age="{mac1.age}" />  
  </mx:FormItem>

 

3、第二种方式似乎仍然不是太满意,能直接绑定一个对象或许会更好

  这是我们的MyGroup.mxml中的代码要发生改变

 

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
		 xmlns:s="library://ns.adobe.com/flex/spark" 
		 xmlns:mx="library://ns.adobe.com/flex/mx"  width="250" height="108">
	<fx:Script>
		<![CDATA[
			//要加上public修饰外边文件方可使用到
			[Bindable]
			public var mac:MyActionScript;
		]]>
	</fx:Script>
	<mx:Form x="81" y="63" width="220" height="88">
		<mx:FormItem label="姓名:">
			<s:TextInput text="{mac.fileName}" />
		</mx:FormItem>
		<mx:FormItem label="年龄:">
			<s:TextInput text="{mac.age}"/>
		</mx:FormItem>
	</mx:Form>
</s:Group>

 

这次MyActionScript.as不变,main.mxml中具体使用如下

  

   先在<fx:Script>中实例化对象

  [Bindable]   
   private var mac:MyActionScript=new MyActionScript("蔡依林","30");
    [Bindable]
   private var mac1:MyActionScript=new MyActionScript("王力宏","31");

   然后具体使用代码为

  <mx:FormItem>
   <components:MyGroup mac="{mac}"/>  
   <components:MyGroup mac="{mac1}"/>  
  </mx:FormItem>

 

4.最终效果图

 

 


 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值