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

dw1jzc5e  于 2022-11-01  发布在  Apache
关注(0)|答案(3)|浏览(142)

我尝试使用以下代码从外部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();
}

我基本上想继续使用默认的样式w/o的用户看到的错误,如果这个文件不能被加载-但我似乎找不到任何方法来做到这一点。

clj7thdc

clj7thdc1#

有趣的问题。请尝试移除removeEventListener调用,或将其注解掉;在我的简短测试中,事件处理程序似乎被调用了两次(我不能立即确定为什么,尽管我怀疑这与样式继承有关),并且注解该行起到了作用。
如果你得到了同样的结果,你可以试着先检查监听器(使用hasEventListener),然后再把它附加到你的loadStyles()函数中。希望它能有所帮助!

k97glaaz

k97glaaz2#

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

仅供参考,这是mx.styles.StyleManagerImpl源代码中的ActionScript代码,当您调用StyleManager.loadStyleDeclarations()时运行该代码。“),并捕获了断点。我认为不应该在此处捕获断点,而是应该运行前面的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;
}
ztigrdn8

ztigrdn83#

我调试了源代码,发现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();
}

相关问题