Css 在从if加载样式(或捕获错误)之前,如何测试SWF URL?

Css 在从if加载样式(或捕获错误)之前,如何测试SWF URL?,css,apache-flex,url,actionscript,Css,Apache Flex,Url,Actionscript,我试图使用以下代码从外部SWF加载样式,但当URL无效时,我不断收到ActionScript错误: Error: Unable to load style(Error #2036: Load Never Completed. URL: http://localhost/css/styles.swf): ../css/styles.swf. at <anonymous>()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\

我试图使用以下代码从外部SWF加载样式,但当URL无效时,我不断收到ActionScript错误:

Error: Unable to load style(Error #2036: Load Never Completed. URL: http://localhost/css/styles.swf): ../css/styles.swf.
at <anonymous>()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\styles\StyleManagerImpl.as:858] 


private function loadStyles(): void
{
    try
    {
        var styleEvent:IEventDispatcher = 
            StyleManager.loadStyleDeclarations("../styles.swf");

        styleEvent.addEventListener(StyleEvent.COMPLETE, 
                                                    loadStyle_completeHandler);

        styleEvent.addEventListener(StyleEvent.ERROR, 
                                                    loadStyle_errorHandler);
    }
    catch (error:Error)
    {
        useDefault();
    }
}

private function loadStyle_completeHandler(event:StyleEvent): void
{
    IEventDispatcher(event.currentTarget).removeEventListener(
        event.type, loadStyle_completeHandler);

    goToNextStep();
}

private function loadStyle_errorHandler(event:StyleEvent): void
{
    IEventDispatcher(event.currentTarget).removeEventListener(
        event.type, loadStyle_errorHandler);

    useDefault();
}
错误:无法加载样式(错误#2036:加载从未完成。URL:http://localhost/css/styles.swf):../css/styles.swf。
at()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\styles\StyleManagerImpl.as:858]
私有函数loadStyles():void
{
尝试
{
变量styleEvent:IEventDispatcher=
StyleManager.loadStyleDeclarations(“../styles.swf”);
styleEvent.addEventListener(styleEvent.COMPLETE,
loadStyle_completeHandler);
styleEvent.addEventListener(styleEvent.ERROR,
loadStyle_errorHandler);
}
捕获(错误:错误)
{
使用默认值();
}
}
私有函数loadStyle\u completeHandler(事件:StyleEvent):void
{
IEventDispatcher(event.currentTarget).removeEventListener(
event.type、loadStyle\u completeHandler);
goToNextStep();
}
私有函数loadStyle_errorHandler(事件:StyleEvent):void
{
IEventDispatcher(event.currentTarget).removeEventListener(
event.type、loadStyle\u errorHandler);
使用默认值();
}

我基本上想继续使用默认样式,而不让用户在无法加载此文件时看到错误-但我似乎找不到任何方法来执行此操作。

有趣的问题。尝试删除removeEventListener调用,或对其进行注释;在我的简短测试中,事件处理程序似乎被调用了两次(我不知道为什么,尽管我怀疑这与样式继承有关),并且注释这一行就成功了


如果得到相同的结果,您可以尝试先检查侦听器(使用hasEventListener),然后再将其附加到loadStyles()函数中。希望有帮助

**不是答案,而是更新:

仅供参考,这是mx.styles.StyleManagerImpl源代码中的ActionScript代码,在调用StyleManager.loadStyleDeclarations()时运行。我运行调试器并在第858行(“抛出新错误(errorText);”)添加了一个断点,该断点被捕获。我认为不应该在那里捕获它,但是应该运行前面的IF(“IF(IF(styleEventDispatcher.willTrigger(StyleEvent.ERROR))”)

public function loadStyleDeclarations2(
                    url:String, update:Boolean = true,
                    applicationDomain:ApplicationDomain = null,
                    securityDomain:SecurityDomain = null):
                    IEventDispatcher
{
    var module:IModuleInfo = ModuleManager.getModule(url);

    var readyHandler:Function = function(moduleEvent:ModuleEvent):void
    {
        var styleModule:IStyleModule =
            IStyleModule(moduleEvent.module.factory.create());

        styleModules[moduleEvent.module.url].styleModule = styleModule;

        if (update)
            styleDeclarationsChanged();
    };

    module.addEventListener(ModuleEvent.READY, readyHandler,
                            false, 0, true);

    var styleEventDispatcher:StyleEventDispatcher =
                                    new StyleEventDispatcher(module);

    var errorHandler:Function = function(moduleEvent:ModuleEvent):void
    {
        var errorText:String = resourceManager.getString(
            "styles", "unableToLoad", [ moduleEvent.errorText, url ]);

        if (styleEventDispatcher.willTrigger(StyleEvent.ERROR))
        {
            var styleEvent:StyleEvent = new StyleEvent(
                StyleEvent.ERROR, moduleEvent.bubbles, moduleEvent.cancelable);
            styleEvent.bytesLoaded = 0;
            styleEvent.bytesTotal = 0;
            styleEvent.errorText = errorText;
            styleEventDispatcher.dispatchEvent(styleEvent);
        }
        else
        {
            throw new Error(errorText);
        }
    };

    module.addEventListener(ModuleEvent.ERROR, errorHandler,
                            false, 0, true);

    styleModules[url] =
        new StyleModuleInfo(module, readyHandler, errorHandler);

    // This Timer gives the loadStyleDeclarations() caller a chance
    // to add event listeners to the return value, before the module
    // is loaded.
    var timer:Timer = new Timer(0);
    var timerHandler:Function = function(event:TimerEvent):void
    {
        timer.removeEventListener(TimerEvent.TIMER, timerHandler);
        timer.stop();
        module.load(applicationDomain, securityDomain);
    };

    timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true);

    timer.start();

    return styleEventDispatcher;
}

我调试了源代码,发现错误事件被触发了两次。因此,我只需在第一次触发错误事件处理程序时设置一个标志,并在继续之前检查该标志的值是否为true:

private var isErrorTriggered:Boolean; // Default is false

private function loadStyle_completeHandler(event:StyleEvent):void
{
    IEventDispatcher(event.currentTarget).removeEventListener(
        event.type, loadStyle_completeHandler);

    goToNextStep();
}

private function loadStyle_errorHandler(event:StyleEvent):void
{
    if (isErrorTriggered)
        return;

    isErrorTriggered = true;

    useDefault();
}