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

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

问题描述

我正在尝试检测弹出窗口何时可见(如果可能,包括工具提示).原因是当弹出窗口出现时,我需要隐藏或冻结(捕获快照)Stage* 组件(StageWebView、StageVideo、StageText 等).

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:

  • 创建自定义弹出管理器
  • 让它在应用程序上调度事件,这样你就可以在任何地方收听
  • 告诉 Flex 使用您的类而不是默认实现

创建自定义 PopupManager

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

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

}

我们重写 addPopup 方法以在显示弹出窗口时调度冒泡事件.暂时忽略 getInstance() 方法.稍后我会回到那个.您需要知道的是 FlashBuilder 不会自动管理您的某些导入,因为这些类被标记为隐藏.没什么可担心的,但您必须手动编写导入语句:

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;

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

这将相当容易:

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

唯一的问题是 Flex 已经注册了一个实现并且您不能覆盖它,即使您在预初始化"上执行它也是如此.所以我们必须在 Flex 开始引导之前完成它.为此,我们将使用自定义预加载器:

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 是默认的 Flex 预加载器.我们只需添加额外的注册代码.现在不要忘记告诉您的应用程序使用这个预加载器:

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" >

现在只需监听事件

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

额外信息

现在为什么 MyPopupManager 必须有一个静态的 getInstance() 方法?那是因为我们用来注册我们的实现的单例类,期望它注册的每个类都是单例,因此有一个名为getInstance"的方法.它会尝试调用这个方法,如果它不存在就会崩溃.如果你不知道什么是单身人士,就谷歌一下.您会发现大量信息.

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:为了解决这个问题,我实际上学到了一些新东西(谢谢).

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

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

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