我有一个界面IBaseInterface
和一个班级BaseClass
。
当我通过BaseClass
类型引用IBaseInterface
时,不会触发与事件名称的绑定。但是正常绑定(没有事件名称)会触发。
如果我使用BaseClass
或Object
类型引用BaseClass
,则一切正常,并且绑定即会触发。
IBaseInterface:
[Event(name="propTwoChanged", type="flash.events.Event")]
[Event(name="propThreeChanged", type="flash.events.Event")]
[Bindable]
public interface IBaseInterface extends IEventDispatcher{
function get propOne() :Number;
function set propOne(value:Number) :void;
[Bindable(event="propTwoChanged")]
function get propTwo() :Number;
[Bindable(event="propThreeChanged")]
function get propThree() :Number;
}
BaseClass的:
[Event(name="propTwoChanged", type="flash.events.Event")]
[Event(name="propThreeChanged", type="flash.events.Event")]
[Bindable]
public class BaseClass extends EventDispatcher implements IBaseInterface{
private var _propOne:Number = 0;
public function get propOne() :Number{
return _propOne;
}
public function set propOne(value:Number) :void{
_propOne = value;
dispatchEvent(new Event('propTwoChanged'));
dispatchEvent(new Event('propThreeChanged'));
}
[Bindable(event="propTwoChanged")]
public function get propTwo() :Number{
return propOne * 2;
}
[Bindable(event="propThreeChanged")]
public function get propThree() :Number{
return propOne / 2;
}
}
所以,澄清问题:
propTwo
和propThree
IBaseInterface
绑定不会触发。propOne
绑定没问题,但没有事件名称。答案 0 :(得分:2)
我找到了两个解决此问题的方法:
在接口上定义单个[Bindable(event="...")]
元数据(不在函数签名上),然后调度单个事件以更新接口上的所有属性。
<强> IBaseInterface
强>
[Event(name="updateBindings", type="flash.events.Event")]
[Bindable(event="updateBindings")]
public interface IBaseInterface extends IEventDispatcher{
function get propOne() :Number;
function set propOne(value:Number) :void;
function get propTwo() :Number;
function get propThree() :Number;
}
<强> BaseClass
强>
[Event(name="updateBindings", type="flash.events.Event")]
[Bindable(event="updateBindings")]
public class BaseClass extends EventDispatcher implements IBaseInterface{
private var _propOne:Number = 0;
public function get propOne() :Number{
return _propOne;
}
public function set propOne(value:Number) :void{
_propOne = value;
dispatchEvent(new Event('updateBindings'));
}
public function get propTwo() :Number{
return propOne * 2;
}
public function get propThree() :Number{
return propOne / 2;
}
}
我觉得这有点笨拙,如果视图中有很多属性或很多听众,那么资源太多了。
在界面上定义单个[Bindable]
(无事件),然后从类中直接调度PropertyChangeEvent
。
<强> IBaseInterface
强>
[Bindable]
public interface IBaseInterface extends IEventDispatcher{
function get propOne() :Number;
function set propOne(value:Number) :void;
function get propTwo() :Number;
function get propThree() :Number;
}
<强> BaseClass
强>
[Bindable]
public class BaseClass extends EventDispatcher implements IBaseInterface{
private var _propOne:Number = 0;
public function get propOne() :Number{
return _propOne;
}
public function set propOne(value:Number) :void{
_propOne = value;
dispatchEvent(new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE, true, true, PropertyChangeEventKind.UPDATE, 'propTwo'));
dispatchEvent(new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE, true, true, PropertyChangeEventKind.UPDATE, 'propThree'));
}
public function get propTwo() :Number{
return propOne * 2;
}
public function get propThree() :Number{
return propOne / 2;
}
}
我认为这种方式最好,它比选项1强,因为只更新了所需的属性。它还清理代码,大多数元数据可以从类和接口中删除。