2011-03-30 21 views
1

使用PopupManager可以添加/創建/刪除新的彈出框。但是我無法找到一種方法來獲得最頂級的彈出窗口,而無需重寫此類(這是您想爲大型Flex應用程序執行的操作)。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包含了所有的DisplayObject與PopupManager一起顯示。但是,儘管屏幕中不可見,但組件中的許多itemRenderer可能在此列表中。這就是爲什麼我的函數得到從容器繼承的最後一個孩子(您的彈出窗口必須從容器繼承)。

+0

Yah沒有在PopUpManager上編寫擴展(老實說不應該做太多工作,只需要重寫addPopUp和createPopUp就可以跟蹤最後創建的那個,會更高效但可能忽略不計的時間,除非你有1000年的彈出窗口,在這種情況下,你有UI問題:)我認爲這似乎有點合理,如果因爲任何原因你覺得延長PopUpManager會有問題。 – shaunhusain 2011-03-30 19:01:45

回答

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?

+0

感謝您的回覆。我想到了這個解決方案,甚至開始實施它。我停止了,因爲我正在開發一個大型企業應用程序,如果我確實選擇了這個解決方案,那意味着每個跟隨我的人都必須瞭解它並使用該類(CustomPopUpManager)來保持新功能的正常工作。根據經驗,我非常肯定他們不會。所以這個想法是在不重寫PopUpManager類的情況下解決問題。 – kamelito 2011-03-31 13:55:21

+0

我不重寫它,我正在爲它創建一個包裝。如果其他開發人員不打算使用它,那麼你遇到的問題幾乎不能解決任何解決方案:P沒有其他解決方案是準確的IMO。請記住,下拉列表本身就是彈出窗口(你知道組合框),你真的無法從真正的彈出窗口中選出那些。這是我能想到的唯一解決方案。 – 2011-03-31 14:39:59