有没有办法监听弹出式管理器类的事件? [英] Is there a way to listen for events on the pop up manager class?

查看:114
本文介绍了有没有办法监听弹出式管理器类的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检测弹出窗口是否可见(如果可能,还包括工具提示)。原因是当弹出窗口出现时,我需要隐藏或冻结(捕捉快照)Stage *组件(StageWebView,StageVideo,StageText等)。

解决方案

有没有真正简单的方法来实现这一点。你可以做的是这样的:
$ b $ ul

  • 创建一个自定义的弹出窗口管理器

  • 应用程序,所以你可以听取任何地方

  • 告诉Flex使用你的类,而不是默认的实现

  • 创建一个自定义的PopupManager



    我们创建一个自定义的PopupManager类,我们可以添加一些自定义的功能。在你的情况下,在应用程序上派发一个事件可能会很有趣,这样我们就可以从displayList的任何地方收听它。我们将扩展PopUpManagerImpl,它是Flex使用的默认实现。

      public class MyPopupManager extends PopUpManagerImpl {

    private static var instance:IPopUpManager;

    static公共函数getInstance():IPopUpManager
    {
    if(!instance)instance = new MyPopupManager();
    返回实例;


    覆盖public function addPopUp(
    window:IFlexDisplayObject,
    parent:DisplayObject,
    modal:Boolean = false,
    childList: String = null,
    moduleFactory:IFlexModuleFactory = null):void
    {
    super.addPopUp(window,parent,modal,childList,moduleFactory);
    var app:IEventDispatcher =
    IEventDispatcher(FlexGlobals.topLevelApplication);
    app.dispatchEvent(new Event(popupAdded,true));


    $ b $ / code $ / pre
    $ b $ p我们重写addPopup方法来调度一个冒泡事件,每当一个弹出窗口显示。现在忽略getInstance()方法。我会在稍后回顾。你需要知道的是,FlashBuilder不会自动管理你的一些导入,因为这些类被标记为隐藏。没有什么可担心的,但是您必须手动编写导入语句:

      import mx.managers.IPopUpManager; 
    导入mx.managers.PopUpManagerImpl;

    告诉Flex使用您的类而不是默认实现



    这很简单:

      import mx.core.Singleton; 
    Singleton.registerClass(mx.managers :: IPopUpManager,MyPopupManager);

    唯一的问题是Flex已经注册了一个实现,并且不能覆盖它,你执行这个'预初始化'。所以我们必须在Flex开始引导之前这样做。我们将使用一个自定义的预加载器:

      public class RegisteringPreloader extends DownloadProgressBar {

    override public函数initialize():void {
    super.initialize();
    Singleton.registerClass(mx.managers :: IPopUpManager,MyPopupManager); DownloadBrogressBar是默认的Flex预加载器。我们只是添加额外的注册码。

     < 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
    preloader =注册预加载器>

    现在只要听取活动

      addEventListener(popupAdded,onPopupAdded); 
    PopUpManager.addPopUp(new Panel(),this);

    额外信息

    现在为什么MyPopupManager必须有一个静态的getInstance()方法?那是因为我们用来注册我们的实现的Singleton类,期望它注册的每个类都是一个单例,因此有一个叫做getInstance的方法。它会尝试调用这个方法,如果不存在就会崩溃。如果你不知道单身是什么,只是谷歌。你会发现很多信息。



    PS:我实际上学到了一些新的东西来解决这个问题(谢谢)。

    I'm trying to detect when pop ups are visible (including tool tips if possible). The reason is that I need to hide or freeze (capture a snapshot) the Stage* components (StageWebView, StageVideo, StageText etc) when pop ups appear.

    解决方案

    There's no really easy way to achieve this. What you can do is this:

    • create a custom popupmanager
    • make it dispatch events on the Application, so you can listen anywhere
    • tell Flex to use your class instead of the default implementation

    Create a custom PopupManager

    We create a custom PopupManager class to which we can add some custom functionality. In your case it might for instance be interesting to dispatch an event on the Application, so that we can listen to it from everywhere on the displayList. We'll be extending PopUpManagerImpl which is the default implementation used by Flex.

    public class MyPopupManager extends PopUpManagerImpl {
    
        private static var instance:IPopUpManager;
    
        static public function getInstance():IPopUpManager 
        {
            if (!instance) instance = new MyPopupManager();
            return instance;
        }
    
        override public function addPopUp(
            window:IFlexDisplayObject, 
            parent:DisplayObject, 
            modal:Boolean=false, 
            childList:String=null, 
            moduleFactory:IFlexModuleFactory=null):void 
        {
            super.addPopUp(window, parent, modal, childList, moduleFactory);
            var app:IEventDispatcher = 
                IEventDispatcher(FlexGlobals.topLevelApplication);
            app.dispatchEvent(new Event("popupAdded", true));
        }
    
    }
    

    We override the addPopup method to dispatch a bubbling event whenever a popup is shown. Ignore the getInstance() method for now. I'll get back to that later. What you do need to know is that FlashBuilder will not automanage some of your imports because these classes were marked as hidden. Nothing to worry about but you'll have to write the import statements manually for:

    import mx.managers.IPopUpManager;
    import mx.managers.PopUpManagerImpl;
    

    Tell Flex to use your class instead of the default implementation

    This would be fairly easy:

    import mx.core.Singleton;
    Singleton.registerClass("mx.managers::IPopUpManager", MyPopupManager);
    

    The only problem is that Flex has already registered an implementation and you can't override it, even if you execute this on 'preinitialize'. So we'll have to do it before Flex starts bootstrapping. We'll use a custom preloader for that:

    public class RegisteringPreloader extends DownloadProgressBar {
    
        override public function initialize():void {
            super.initialize();
            Singleton.registerClass("mx.managers::IPopUpManager", MyPopupManager);
        }
    
    }
    

    DownloadProgressBar is the default Flex preloader. We just add the extra registering code. Now don't forget to tell your application to use this preloader:

    <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"
                   preloader="RegisteringPreloader" >
    

    Now just listen for the event

    addEventListener("popupAdded", onPopupAdded);
    PopUpManager.addPopUp(new Panel(), this);
    

    Extra info

    Now why does MyPopupManager have to have a static getInstance() method? Well that's because that Singleton class we used to register our implementation, expects every class it registers to be a singleton and hence to have a method called 'getInstance'. It will try to call this method and will crash if it doesn't exist. If you don't know what a singleton is, just google. You'll find tons of information.

    PS: I actually learnt something new trying to solve this question (thanks for that).

    这篇关于有没有办法监听弹出式管理器类的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆