Flex、状态和继承

发布于 2024-08-26 04:01:41 字数 53 浏览 3 评论 0原文

是否可以有一个子类将状态添加到基类中定义的状态集中?目前看来我的子类覆盖了基类中的所有状态。

Is it possible to have a child class which adds states to the set of states which are defined in the base class? Currently it looks like my child class overrides all of the states in the base class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2024-09-02 04:01:41

在您的子组件中,以声明方式创建一个单独的状态数组,并在 preinitialize 事件中将它们连接起来。请参阅此示例。

<!-- MyParent -->
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onCreationComplete()">
    <mx:Script>
        <![CDATA[
            import mx.controls.Button;
            private function onCreationComplete():void {
                for each(var state:State in states) {
                    var button:Button = new Button();
                    button.label = state.name;
                    button.addEventListener(MouseEvent.CLICK, onClick);
                    addChild(button);
                }
            }

            private function onClick(event:MouseEvent):void {
                currentState = Button(event.target).label;
            }
        ]]>
    </mx:Script>
    <mx:states>
        <mx:State name="Red">
            <mx:SetStyle name="backgroundColor" value="#FF0000" />
        </mx:State>
        <mx:State name="Green">
            <mx:SetStyle name="backgroundColor" value="#00FF00" />
        </mx:State>
        <mx:State name="Blue">
            <mx:SetStyle name="backgroundColor" value="#0000FF" />
        </mx:State>
    </mx:states>
</mx:VBox>


<!-- MyChild -->
<?xml version="1.0" encoding="utf-8"?>
<MyParent xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" preinitialize="onPreinitialize()">
    <mx:Script>
        <![CDATA[

            private function onPreinitialize():void {
                states = states.concat(newStates);
            }
        ]]>
    </mx:Script>
    <mx:Array id="newStates">
        <mx:State name="Cyan">
            <mx:SetStyle name="backgroundColor" value="#00FFFF" />
        </mx:State>
        <mx:State name="Purple">
            <mx:SetStyle name="backgroundColor" value="#FF00FF" />
        </mx:State>
    </mx:Array>
</MyParent>


<!-- MyApp -->
<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" 
    minWidth="955" 
    minHeight="600" xmlns="*">

    <MyParent width="50%" height="100%" />
    <MyChild width="50%" height="100%" right="0" />

</mx:Application>

In your child component, create a separate array of states declaratively and in the preinitialize event concatenate them. See this example.

<!-- MyParent -->
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onCreationComplete()">
    <mx:Script>
        <![CDATA[
            import mx.controls.Button;
            private function onCreationComplete():void {
                for each(var state:State in states) {
                    var button:Button = new Button();
                    button.label = state.name;
                    button.addEventListener(MouseEvent.CLICK, onClick);
                    addChild(button);
                }
            }

            private function onClick(event:MouseEvent):void {
                currentState = Button(event.target).label;
            }
        ]]>
    </mx:Script>
    <mx:states>
        <mx:State name="Red">
            <mx:SetStyle name="backgroundColor" value="#FF0000" />
        </mx:State>
        <mx:State name="Green">
            <mx:SetStyle name="backgroundColor" value="#00FF00" />
        </mx:State>
        <mx:State name="Blue">
            <mx:SetStyle name="backgroundColor" value="#0000FF" />
        </mx:State>
    </mx:states>
</mx:VBox>


<!-- MyChild -->
<?xml version="1.0" encoding="utf-8"?>
<MyParent xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" preinitialize="onPreinitialize()">
    <mx:Script>
        <![CDATA[

            private function onPreinitialize():void {
                states = states.concat(newStates);
            }
        ]]>
    </mx:Script>
    <mx:Array id="newStates">
        <mx:State name="Cyan">
            <mx:SetStyle name="backgroundColor" value="#00FFFF" />
        </mx:State>
        <mx:State name="Purple">
            <mx:SetStyle name="backgroundColor" value="#FF00FF" />
        </mx:State>
    </mx:Array>
</MyParent>


<!-- MyApp -->
<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" 
    minWidth="955" 
    minHeight="600" xmlns="*">

    <MyParent width="50%" height="100%" />
    <MyChild width="50%" height="100%" right="0" />

</mx:Application>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文