Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3245430
  • 博文数量: 530
  • 博客积分: 13360
  • 博客等级: 上将
  • 技术积分: 5473
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-13 13:32
文章分类

全部博文(530)

文章存档

2017年(1)

2015年(2)

2013年(24)

2012年(20)

2011年(97)

2010年(240)

2009年(117)

2008年(12)

2007年(8)

2006年(9)

分类:

2009-12-21 11:29:27

关于Flex2绑定的例子,大都是使用[Bindable]注释,或在mx视图组件中使用大括号{}来实现的。基本都是象下面的代码
     
     
           
        
           [CDATA[  
                  
                [Bindable]  
                private var myText : String;  
           ]]  
      
 
         
        
        
        
         
  
 

但实际项目中,并非都是这种理想的情况,有时你的视图组件是动态生成的,有时你需要动态的改变绑定,有时你使用Sprite动态生成的图形也需要绑定数据,或者你就是喜欢完全使用AS来写。
这时就可以使用mx.binding.utils.BindingUtils类,改写上面的例子

    
     
          
         
            [CDATA[  
                import mx.controls.Label;  
                import mx.controls.TextInput;  
                import mx.binding.utils.BindingUtils;  
                  
               private function init():void {  
                   var textInput : TextInput = new TextInput();  
                   var textLabel : Label = new Label();  
                     
                   myBox.addChild(textInput);  
                   myBox.addChild(textLabel);  
                     
                   BindingUtils.bindProperty(textLabel, "text", textInput, "text");  
               }  
                 
           ]]  
      
 
         
        
         
  
 
这个例子将textInput.text与textLabel.text进行了绑定,而且没有使用[Bindable],也没有使用{}。
使用这种方法,可以将多个视图组件与一个Value Object对象(或叫DTO、Bean等)进行绑定,当VO对象改变时,所有绑定的视图都会改变。也可以在纯as文件中实现动态绑定了。
xml 代码

     
     
          
         
          [CDATA[  
               import model.UserVO;  
                import mx.controls.Label;  
                import mx.controls.TextInput;  
               import mx.binding.utils.BindingUtils;  
                 
               private var userVO : UserVO = new UserVO();  
                
               private function init():void {  
                     
                   for (var i : int = 0; i < 3; i++) {  
                       var nameLabel : Label = new Label();  
                       var emailLabel : Label = new Label();  
                       myBox.addChild(nameLabel);  
                       myBox.addChild(emailLabel);  
                         
                       BindingUtils.bindProperty(nameLabel, "text", userVO, "name");  
                       BindingUtils.bindProperty(emailLabel, "text", userVO, "email");  
                   }  
               }  
                 
           ]]  
      
 
        
            
            
      
 
       
  
 

    package model  
    {  
        [Bindable]  
        public class UserVO  
        {  
            public var name: String;  
            public var email: String;  
           public var online: Boolean = false;  
       }  
   } 

参考文献
1.Flex2使用BindingUtils动态绑定. http://wangcheng.javaeye.com/blog/116865
 
阅读(3399) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~