Flex :找到最上面的弹出窗口

发布于 2024-10-28 01:43:42 字数 953 浏览 5 评论 0原文

使用 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 继承的最后一个子级(您的弹出窗口必须从 Container 继承)。

With the PopupManager it's possible to add/create/remove a new popup. But i can't find a way to get the top most popup without overriding this class (which something you want to do for a big Flex application).

So far I found this solution, which is more kinna of work around. So if any body has a better solution, i will be pretty much happy to read it.

Assuming the you call the addPopup/createPopup with the parameter PopUpManagerChildList.POPUP, example :

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

The this function will return the top most 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 contains all the DisplayObject displayed with PopupManager. But many of the itemRenderers of your components could be in this list eventhough there are not visible in the screen. This is why my function get the last child inheriting from Container (your popup must inherit from Container).

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

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

发布评论

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

评论(1

终止放荡 2024-11-04 01:43:42

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

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 处的弹出窗口。

理解者?

What you need to do is create a custom popup manager. For the sake of it, I'll give you mine. I don't have the 'topmost' function, but you could easily add it. Here's the class:

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];
            }
        }


    }
}

This should get you started. From here, if you want to implement your function, you could create an array that would add/remove popup when you create/remove them (every new popup gets added to index 0) and the function would just return the popup at index 0.

Comprender?

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