Flex:找到最顶层的弹出窗口

时间:2011-03-30 16:53:40

标签: flex flex3 flex4

使用PopupManager可以添加/创建/删除新的弹出窗口。但我找不到一种方法来获得最顶级的弹出窗口而不覆盖这个类(你想要为一个大的Flex应用程序做些什么)。

到目前为止,我找到了这个解决方案,这是更多的解决方案。因此,如果任何身体有更好的解决方案,我将非常乐意阅读它。

假设您使用参数PopUpManagerChildList.POPUP调用addPopup / createPopup,例如:

PopUpManager.createPopUp(parent,MyPopupClass,true,PopUpManagerChildList.POPUP);

此函数将返回最顶部的弹出窗口:

private function getTopMostPopup():void
{

     var childList:IChildList = Application.application.systemManager.popUpChildren;
     for (var i:int = childList.numChildren - 1; i > 0; i--)
     {
          var child:DisplayObject = childList.getChildAt( i );
          if (child is Container)
               return child;
     }
     return null;
}

Application.application.systemManager.popUpChildren包含PopupManager显示的所有DisplayObject。但是,如果屏幕上看不到,那么组件的许多itemRenderer都可能在此列表中。这就是为什么我的函数获取继承自Container的最后一个子元素(你的popup必须从Container继承)。

1 个答案:

答案 0 :(得分:2)

您需要做的是创建自定义弹出管理器。为了它,我会给你我的。我没有“最顶层”功能,但您可以轻松添加它。这是班级:

package com.michelboudreau.utils {

    import flash.display.DisplayObject;
    import flash.utils.Dictionary;

    import mx.collections.ArrayCollection;
    import mx.core.IFlexDisplayObject;
    import mx.managers.PopUpManager;

    public class CustomPopUpManager {

        private static var popUps:Dictionary = new Dictionary();

        public function CustomPopUpManager()
        {
            throw new Error("CustomPopUpManager is a static class. Cannot create instance.");
        }

        /**
         * Creates a top-level window and places it above other windows in the z-order.
         * 
         * @param id:String - the id of the pop up
         * @param container:DisplayObject - DisplayObject to be used for determining which SystemManager's layers to use and optionally the reference point for centering the new top level window. It may not be the actual parent of the popup as all popups are parented by the SystemManager.
         * @param className:Class - Class of object that is to be created for the popup. The class must implement IFlexDisplayObject.
         * @param modal:Boolean(default = false) — If true, the window is modal which means that the user will not be able to interact with other popups until the window is removed
         * @param center:Boolean(default = false) - Centers a popup window over whatever window was use as container
         * @param childList:String (default = null) — The child list in which to add the popup. One of PopUpManagerChildList.APPLICATION, PopUpManagerChildList.POPUP, or PopUpManagerChildList.PARENT (default). 
         * 
         * @return IFlexDisplayObject — Reference to new top-level window
         */
        public static function createPopUp(id:String, container:DisplayObject, className:Class, modal:Boolean = false, center:Boolean = false, childList:String = null ):IFlexDisplayObject
        {
            if (getPopUpByID(id))
            {
                return getPopUpByID(id);
            }else{
                var popUp:IFlexDisplayObject = PopUpManager.createPopUp(container, className, modal, childList);

                if (center)
                {
                    PopUpManager.centerPopUp(popUp);
                }

                popUps[id] = popUp;
                return popUp;
            }

        }

        /**
         * Returns a IFlexDisplayObject based on the specified id.
         * 
         * @param id:String - the id of the pop up
         * 
         * @return IFlexDisplayObject — Reference to new top-level window
         */
        public static function getPopUpByID(id:String):IFlexDisplayObject 
        {
            return popUps[id];
        }

        /**
         * Removes all pop ups
         * 
         * @return void
         */
        public static function removeAll():void 
        {
            popUps = new Dictionary();
        }

        /**
         * Removes a popup window popped up by id.
         * 
         * @param id:String - the id of the pop up
         * 
         */
        public static function removePopUpByID(id:String):IFlexDisplayObject 
        {
            var popup:IFlexDisplayObject = getPopUpByID(id);
            if(popup)
            {
                PopUpManager.removePopUp(popup);
                removePopUpData(id);
            }
            return popup;
        }

        /**
         * Removes pop up based on IFlexDisplayObject
         * 
         * @param popUp:IFlexDisplayObject - the pop up to be removed
         */
        public static function removePopUp(popUp:IFlexDisplayObject):void 
        {
            // Always try to remove popup no matter what
            PopUpManager.removePopUp(popUp);

            // Find popup and remove from Dictionary
            for(var id:String in popUps)
            {
                if(popUps[id] == popUp)
                {
                    removePopUpData(id);
                    break;
                }
            }
        }

        /**
         * Removes the pop up data
         * 
         * @param id:String - the id of the pop up
         */
        private static function removePopUpData(id:String):void 
        {
            if(popUps[id])
            {
                popUps[id] = null;
                delete popUps[id];
            }
        }


    }
}

这应该让你开始。从这里开始,如果你想实现你的函数,你可以创建一个数组,当你创建/删除它们时添加/删除弹出窗口(每个新的弹出窗口都被添加到索引0),该函数只返回索引0处的弹出窗口。

Comprender?