绑定不在接口上触发

时间:2013-05-02 15:43:14

标签: actionscript-3 flex binding flex-spark

我有一个界面IBaseInterface和一个班级BaseClass

当我通过BaseClass类型引用IBaseInterface时,不会触发与事件名称的绑定。但是正常绑定(没有事件名称)会触发。

如果我使用BaseClassObject类型引用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;
    }

}

所以,澄清问题:

  • propTwopropThree IBaseInterface绑定不会触发。
  • propOne绑定没问题,但没有事件名称。
  • 只有在通过界面访问时才会出现问题,其他类型都可以。

1 个答案:

答案 0 :(得分:2)

我找到了两个解决此问题的方法:

选项1

在接口上定义单个[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;
    }

}

我觉得这有点笨拙,如果视图中有很多属性或很多听众,那么资源太多了。


选项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强,因为只更新了所需的属性。它还清理代码,大多数元数据可以从类和接口中删除。