简体   繁体   English

如何为州动态创建Flex 4 AddChild操作?

[英]How do I dynamically create Flex 4 AddChild actions for States?

I have an application in which I need to create mx.states.State objects on the fly, as I'm reading external data in order to create the states. 我有一个应用程序,需要在运行中创建mx.states.State对象,因为我正在读取外部数据以创建状态。 Each State only has a single child, so here's my code which I was using to accomplish this: 每个State只有一个孩子,所以这是我用来完成此任务的代码:

var state:State = new State();
state.name = "a";
state.overrides = [new AddChild(parent, DisplayObject(view))];
this.states.push(state);

However, when I actually change a state, I get a runtime error relating to the fact that you can't call addChild on a spark.components.Group component. 但是,当我实际更改状态时,我收到一个运行时错误,原因是您无法在spark.components.Group组件上调用addChild Is there an equivalent AddElement action for adding elements to a Group during a state change? 是否存在等效的AddElement操作,用于在状态更改期间将元素添加到Group

You'll have to use the AddItems class for spark elements. 您必须将AddItems类用于spark元素。 I have provided an answer to a similar question before, it might be helpful. 之前,我已经为类似问题提供了答案 ,这可能会有所帮助。

Solution was simple, but a big question remains: why doesn't the Flex SDK include a class like this by default? 解决方案很简单,但仍然存在一个大问题:默认情况下,Flex SDK为什么不包括这样的类? And how does Flex 4 accomplish this without using a class like below? 而Flex 4如何在不使用下面的类的情况下完成此任务?

In any case, here's the class. 无论如何,这是课程。

package mx.states {
    import mx.core.IVisualElementContainer;
    import mx.core.IVisualElement;
    import mx.core.UIComponent;
    import mx.states.IOverride;
    import mx.utils.OnDemandEventDispatcher;

    /**
     * @author rfkrocktk
     */
    public class AddElement extends OnDemandEventDispatcher implements IOverride {

        private var _target:IVisualElement;

        private var _relativeTo:IVisualElementContainer;

        public function AddElement(relativeTo:IVisualElementContainer = null,
                target:IVisualElement = null) {
            this.relativeTo = relativeTo;
            this.target = target;
        }

        public function initialize() : void {

        }

        public function apply(parent : UIComponent) : void {
            this.relativeTo.addElement(this.target);
        }

        public function remove(parent : UIComponent) : void {
            this.relativeTo.removeElement(this.target);
        }

        [Bindable]
        public function get target():IVisualElement {
            return _target;
        }

        public function set target(value:IVisualElement):void {
            _target = value;
        }

        [Bindable]
        public function get relativeTo() : IVisualElementContainer {
            return _relativeTo;
        }

        public function set relativeTo(relativeTo : IVisualElementContainer) : void {
            _relativeTo = relativeTo;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM