TitleWindow中的Flex分离事件不是系统范围的吗?

时间:2011-06-07 08:07:57

标签: flex events

我有一个树,它是childs dispaching事件,如果我在TitleWindow之外运行对象 - 一切正常,但是如果我将它封装在TitleWindow中作为下面的源 - 事件不再在系统范围内被分派。< / p>

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               xmlns:sparkTree="com.sparkTree.*"
               creationComplete="init()">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.managers.PopUpManager;

            private function init():void
            {
                PopUpManager.addPopUp( xWin, this );
                this.addEventListener( MyEvent.REQUEST_FORWARD, function( e : MyEvent ):void{ Alert.show( ":P" ) } );
            }

        ]]>
    </fx:Script>

    <s:TitleWindow id="xWin" >

        <s:HGroup width="100%" height="100%">
            <sparkTree:Tree labelField="label" 
                    dataProvider="{dataProviderXML}" 
                    width="300" height="500" 
                    textRollOverColor="yellow" 
                    textSelectedColor="0xFFFFFF"
                    itemRenderer="com.sparkTree.XItemRenderer">

                <sparkTree:layout>
                    <s:VerticalLayout gap="0" variableRowHeight="true"/>
                </sparkTree:layout>
            </sparkTree:Tree>
        </s:HGroup>
    </s:TitleWindow>
</s:Application>

1 个答案:

答案 0 :(得分:3)

更改您的

this.addEventListener( MyEvent.REQUEST_FORWARD, function( e : MyEvent ):void{ Alert.show( ":P" ) } );

xWin.addEventListener( MyEvent.REQUEST_FORWARD, function( e : MyEvent ):void{ Alert.show( ":P" ) } );

或许你正在使用事件冒泡?

我不知道有关您的代码的详细信息并且看到它很成问题但是这段代码运行正常:

<?xml version="1.0" encoding="utf-8"?>
<s:Application creationComplete="init()" xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark">
    <fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.managers.PopUpManager;

        protected function init():void
        {
            PopUpManager.addPopUp(xWin, this);
            systemManager.addEventListener(MyEvent.REQUEST_FORWARD, function(e:MyEvent):void
            {
                Alert.show(":P")
            });
        }
    ]]>
    </fx:Script>
    <fx:Declarations>
        <s:TitleWindow id="xWin">
            <s:Button click="xWin.dispatchEvent(new MyEvent(MyEvent.REQUEST_FORWARD))" horizontalCenter="0"
                verticalCenter="0" />
        </s:TitleWindow>
    </fx:Declarations>
</s:Application>

MyEvent的位置:

package
{
import flash.events.Event;

public class MyEvent extends Event
{
    public static const REQUEST_FORWARD:String = "requestForward";

    public function MyEvent(type:String)
    {
        super(type, true, false);
    }
}
}

但我个人建议您更改所有代码:)

首先,我强烈建议您在单独的组件中提取窗口(您应该一直这样做并仅使用内联组件进行原型设计):

<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Metadata>
        [Event(name="requestForward", type="MyEvent")]
    </fx:Metadata>
    <s:Button click="dispatchEvent(new MyEvent(MyEvent.REQUEST_FORWARD))" horizontalCenter="0"
        verticalCenter="0" />
</s:TitleWindow>

其次,不要使用冒泡事件而不需要特别是外部组件:

package
{
import flash.events.Event;

public class MyEvent extends Event
{
    public static const REQUEST_FORWARD:String = "requestForward";

    public function MyEvent(type:String)
    {
        super(type);
    }
}
}

第三,尽量不要使用内部函数并创建单独的方法。它更具可读性,更容易取消订阅。所以我们的最终申请:

<?xml version="1.0" encoding="utf-8"?>
<s:Application creationComplete="init()" xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark">
    <fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.managers.PopUpManager;

        protected function init():void
        {
            var win:MyWindow = MyWindow(PopUpManager.createPopUp(this, MyWindow));
            win.addEventListener(MyEvent.REQUEST_FORWARD, win_requestForwardHandler);
        }

        private function win_requestForwardHandler(event:MyEvent):void
        {
            Alert.show(":P");
            var win:MyWindow = MyWindow(event.currentTarget);
            win.removeEventListener(MyEvent.REQUEST_FORWARD, win_requestForwardHandler);
        }
    ]]>
    </fx:Script>
</s:Application>