簡體   English   中英

從運行時加載的swf實例化類時出錯

[英]Error instantiating classes from runtime loaded swf

我有以下幾點:

        protected function caller(event:FlexEvent):void
        {
            var r:URLRequest=new URLRequest('http://remote/TESTLibrary.swf');
            var c:LoaderContext=new LoaderContext();
            c.applicationDomain=ApplicationDomain.currentDomain;
            var l:Loader=new Loader();
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, hndComplete);
            l.load(r, c);
        }


        protected function hndComplete(event:Event):void
        {
            var d:ArrayCollection; //not used but here for
            var cls:Class=(event.target as LoaderInfo).applicationDomain.getDefinition('ro.vnc.modules.ModuleManager') as Class;
            var instance:Object=new cls();
        }

庫中包含的類:

package ro.vnc.modules
{
    import mx.collections.ArrayCollection;

    public class ModuleManager
    {
        public function ModuleManager()
        {
            var d:ArrayCollection;//if commented works fine
            var c:Number=5;
            trace('ModuleManager', c);
        }
    }
}

如果我評論d:ArrayCollection eveything的定義工作正常,但我使用了mx.collections之類的全局可訪問軟件包之外的類,則會收到VerifyError:錯誤#1014:找不到mx.collections :: ArrayCollection類。 任何幫助將不勝感激。

在這種情況下,該類型是導入的,因為您可以看到是否更注意舊的加載器。 但是我寫了另一種加載swf的方法,效果很好:

    public function ModulesInstaller()
    {
        var f:File=new File();

        f.addEventListener(Event.SELECT, hndSelect);
        f.browseForOpen('Select Library', [new FileFilter('Library file', '*.swf')]);
    }


    protected function hndSelect(event:Event):void
    {
        var fs:FileStream=new FileStream();
        fs.open(event.target as File, FileMode.READ);
        var bytes:ByteArray=new ByteArray();
        fs.readBytes(bytes);
        var lc:LoaderContext=new LoaderContext();
        lc.allowCodeImport=true;
        var l:Loader=new Loader();
        l.contentLoaderInfo.addEventListener(Event.INIT, hndLoaded);
        l.loadBytes(bytes, lc);
        l.loaderInfo

    }

    private function hndLoaded(event:Event):void
    {
            var clsRef:Class=(event.target as LoaderInfo).applicationDomain.getDefinition('classRefName') as Class;
    }

問題是您在引用ArrayCollection時沒有先將其導入調用方中。 我確切地知道您要嘗試做什么-在運行時將其拉入以用於標准用法,但Flash不允許這樣做。 如果沒有明確引用類,閃存將被迫猜測“ ArrayCollection”是什么-因此它會在編譯時拋出驗證錯誤。 我嘗試了幾種不同的類類型=及其標准行為。

注意-如果您在被調用方中實例化該類,則可以在調用方中獲得對正確鍵入對象的引用:

package ro.vnc.modules
{
    import mx.collections.ArrayCollection;

    public class ModuleManager
    {

        public var d:ArrayCollection;

        public function ModuleManager()
        {
            d = new ArrayCollection();
            var c:Number=5;
            trace('ModuleManager', c);
        }
    }
}

但是,只要您在調用方中將其作為類型引用,就會收到驗證錯誤。

 protected function hndComplete(event:Event):void
            {
                var cls:Class=(event.target as LoaderInfo).applicationDomain.getDefinition('ro.vnc.modules.ModuleManager') as Class;
                var instance:Object=new cls();
                trace(instance.d); //[object ArrayCollection]
                var d1:ArrayCollection = instance.d; // throws verify error
                var d2:* = (event.target as LoaderInfo).applicationDomain.getDefinition(instance.d); // throws reference error
            }

解決方案是將類型導入調用類中。 (盡管我很樂意看到有人針對這種情況提出了解決方案,該解決方案無需額外導入即可...)

干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM