Event:
1.事件派发 Dispatching
Flash.events.EventDispacher类提供了事件dispatch函数,如果需要dispatch事件,则应该考虑继承该类或其子类.如果不能继承该类,则应事先IEventDispatcher接口
2.监听事件 Listening to Events
在监听事件时应首先考虑是否要使用weak referenced.如果事件监听与派发与监听函数有共同的生命周期,则可以使用strong referenced,在其他的大部分情况下,则应使用weak referenced.
需要注意的是, 如果事件派发者属性为Public, 则在任何允许的地方都可以增加监听函数, 从而达到低内聚, 更易于操作.
Bindable的三种实现方式:
1.使用Event实现.
2.MXML中使用大括号实现.
3.AS中使用ChangeWatcher实现.
Event与Bindable举例:
有一个Person类:
package com.insprise
{
import flash.events.Event;
import flash.events.EventDispatcher;
import mx.events.PropertyChangeEvent;
public class Person extends EventDispatcher
{
private var _name:String;
private var _age:int;
private var _desc:String;
/**
* 构造函数
*/
public function Person()
{
super();
}
/**
* Get&Set Name SetName时dispatch一个PropertyChangeEvent事件
*/
[Bindable(event="propertyChange")]
public function get name():String {
trace("Get name: " + _name);
return _name;
}
public function set name(name_:String):void {
if(name_ == _name) {
return;
}
var old:String = this._name;
this._name = name_;
trace("Set Name to: " + name_);
dispatchEvent(PropertyChangeEvent.createUpdateEvent(this,"name",old,name_));
}
/**
* Get&Set age; age被Set的时候会dispatch一个ageChanged事件.
*/
[Bindable(event="ageChanged")]
public function get age():int {
trace("Get age: " + _age);
return _age;
}
public function set age(age_:int):void {
if(age_ == _age) {
return;
}
var old:int = this._age;
this._age = age_;
trace("Set Age to: " + age_);
dispatchEvent(new Event("ageChanged"));
}
/**
* Get&Set Desc;desc被set时会自动dispatch一个PropertyChangeEvent.
*/
[Bindable]
public function get desc():String {
trace("Get desc: " + _desc);
return _desc;
}
public function set desc(desc_:String):void {
if(desc_ == _desc) {
return;
}
var old:String = this._desc;
this._desc = desc_;
trace("Set desc to: " + desc_);
}
}
}
现在我们通过上面提到的三种方式实现Bindable: