Flex 自定义事件2

LoginEvent.as
package events
{
    import flash.events.Event;
    import datas.UserInfo;

    public class LoginEvent extends Event
    {
        public 
function  LoginEvent(user:UserInfo,type:String){
            super(type);
            
this .user  =  user;
        }
        public 
var  user:UserInfo;        
    }
}

UserInfo.as
package datas
{
    public class UserInfo
    {
        public 
var  name:String;
        public 
var  password:String;
    }
}

LoginControl.mxml
<? xml version="1.0" encoding="utf-8" ?>
< mx:Canvas  xmlns:mx ="http://www.adobe.com/2006/mxml"  width ="290"  height ="132" >
< mx:Metadata >
    [Event(name="login",type="events.LoginEvent")]
</ mx:Metadata >
< mx:Script >
    
<![CDATA[
        import datas.UserInfo;
        import events.LoginEvent;
        public function login():void
        {
            var user:UserInfo = new UserInfo();
            user.name = this.txtUser.text;
            user.password = this.txtPassword.text;
            
            var event:LoginEvent = new LoginEvent(user,"login");
            this.dispatchEvent(event);
        }
    
]]>
</ mx:Script >
    
< mx:Button  x ="224"  y ="100"  label ="Login"  click ="login()" />
    
< mx:Form  x ="10"  y ="10"  width ="270" >
        
< mx:FormItem  label ="User Name:"  required ="true" >
            
< mx:TextInput  maxChars ="10"  id ="txtUser" />
        
</ mx:FormItem >
        
< mx:FormItem  label ="Password:"  required ="true" >
            
< mx:TextInput  displayAsPassword ="true"  maxChars ="8"  id ="txtPassword" />
        
</ mx:FormItem >
    
</ mx:Form >
    
</ mx:Canvas >

EventTest.mxml
<? xml version="1.0" encoding="utf-8" ?>
< mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml"  layout ="absolute"  xmlns:ns1 ="*"  width ="360"  height ="185" >
< mx:Script >
    
<![CDATA[
        import mx.controls.Alert;
        import datas.UserInfo;
        import events.LoginEvent;
        
        private function loginHandler(event:LoginEvent):void{
            mx.controls.Alert.show(event.user.name +" log in.");
            
        }
    
]]>
</ mx:Script >
    
< ns1:LoginControl  x ="27"  y ="24"  login ="loginHandler(event)"  width ="301"  height ="135" >
    
</ ns1:LoginControl >
    
</ mx:Application >

Flex 自定义事件2

测试:

你可能感兴趣的:(Flex)