Flex/AS3在侦听从Singleton类调度的事件时出现问题

Ton*_*ony 0 apache-flex events singleton flex3 actionscript-3

我有一个单例类用于全局访问配置信息.这个名为ConfigurationData的单例类扩展了EventDispatcher.这是一个类(请注意,我保留了一些像变量声明一样的东西来保持这个简短):

/**
* Dispatched when the config file has been loaded.
*/
[Event (name="configurationLoaded", type="flash.events.Event")]

/**
* Dispatched when the config file has been loaded.
*/
[Event (name="configurationLoadFailed", type="flash.events.Event")]

public class ConfigurationData extends EventDispatcher{
    // Event name constants.
    public static const CONFIGURATION_LOADED:String = "configurationLoaded";
    public static const CONFIGURATION_LOAD_FAILED:String = "configurationLoadFailed";

    // The singleton instance.
    private static var singleton:ConfigurationData;

    /**
    * Don't call the constructor directly, use getInstance() instead.
    */
    public function ConfigurationData(pvt:PrivateClass){
        // init
    }

    /**
     * Get the singleton ConfigurationData. 
     * @return The ConfigurationData instance.
     */
    public static function getInstance():ConfigurationData{
           if ( !ConfigurationData.singleton ) ConfigurationData.singleton = new ConfigurationData(new PrivateClass());
           return ConfigurationData.singleton;
       }

       public function initialize():void{
        var configureService:HTTPService = new HTTPService;
        configureService.url = _config_base_url + _config_path;
        configureService.addEventListener(FaultEvent.FAULT, onConfigureFault);
        configureService.addEventListener(ResultEvent.RESULT, onConfigureResult);
        configureService.send();
       }

       private function onConfigureResult(event:ResultEvent):void{
        var i:int = 0;
        for(i=0; i<event.result.carriers.carrier.length; i++){
            _mobile_carriers.addItem({label:event.result.carriers.carrier[i].name, data:event.result.carriers.carrier[i].id});
        }
        dispatchEvent(new Event(CONFIGURATION_LOADED));
    }

    private function onConfigureFault(event:FaultEvent):void{
        _mobile_carriers = _default_carriers as ArrayCollection;
        dispatchEvent(new Event(CONFIGURATION_LOAD_FAILED));
    }
}

// This class is used to ensure that the ConfigurationData constructor can't be called directly,
// getInstance() must be used instead. 
class PrivateClass {
    public function PrivateClass() {}
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个MXML组件,它侦听CONFIGURATION_LOADED事件:

ConfigurationData.getInstance().addEventListener(Event.CONFIGURATION_LOADED, onConfigurationLoaded);
Run Code Online (Sandbox Code Playgroud)

由于某种原因,这会产生以下错误:1119:通过带有静态类型Class的引用访问可能未定义的属性CONFIGURATION_LOADED.

有谁知道如何解决这个问题所以我可以听听这个事件吗?

谢谢!

Sti*_*ler 6

您正在尝试访问不存在的Event类上的静态const : Event.CONFIGURATION_LOADED.

在这种情况下,您需要创建自定义Event类:

public class ConfigurationEvent extends Event
{   
    public static const CONFIGURATION_LOADED:String = "configurationLoaded";
    public static const CONFIGURATION_LOAD_FAILED:String = "configurationLoadFailed";

    public function ConfigurationEvent(type:String)
    {
         super(type);
    }
}
Run Code Online (Sandbox Code Playgroud)

发送和收听这些自定义事件而不是Event.CONFIGURATION_LOADED:

dispatchEvent(new ConfigurationEvent(ConfigurationEvent.CONFIGURATION_LOAD_FAILED));

ConfigurationData.getInstance().addEventListener(ConfigurationEvent.CONFIGURATION_LOADED, onConfigurationLoaded);
Run Code Online (Sandbox Code Playgroud)