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

时间:2009-01-15 22:38:57

标签: css 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\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();
}

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

3 个答案:

答案 0 :(得分:1)

有趣的问题。尝试删除removeEventListener调用,或将其注释掉;在我的简短测试中,似乎事件处理程序被调用了两次(我不能立即确定原因,虽然我怀疑它与样式继承有关),并且评论该行已经成功了。

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

答案 1 :(得分:0)

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

仅供参考,这是调用StyleManager.loadStyleDeclarations()时运行的mx.styles.StyleManagerImpl源代码中的ActionScript代码。我运行调试器并在第858行添加了断点(“抛出新错误(errorText);”),并且捕获了断点。我认为它不应该被捕到,但应该运行之前的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;
}

答案 2 :(得分:0)

我调试了源代码,发现ERROR事件 被触发两次。所以,我只是在第一次触发ERROR事件处理程序时设置一个标志,并在继续之前检查该标志值为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();
}