在运行时在动作脚本中嵌入图像

时间:2012-04-25 13:18:54

标签: flex actionscript embed

我必须使用类似的东西来创建图像对象

[Embed("/assets/images/Header.png")]  
public static var HeaderIcon:Class;  

但是现在embedd中的字符串是从某些xml中动态引入的 像xmlObject.child("icon");一样,我试过它

[Embed(xmlObject.child('icon').toString())]  
public static var HeaderIcon:Class;  

然而,它会出现错误,如

  

无效元数据

我在动作脚本中使用上面的代码(很明显) 有什么方法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

您需要使用Loader类:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html

它比嵌入式资源要复杂一些,但文档中有很好的例子。

答案 1 :(得分:0)

班级名称:-HeaderIconClass

//////////

package
{
    import spark.components.Image;

    internal dynamic class HeaderIconClass extends Image
    {
        public function HeaderIconClass()
        {
            super();
        }
    }
}

在组件或应用程序级别内: -

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               creationComplete="application1_creationCompleteHandler(event)">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            [Bindable]
            public var headerIcon:HeaderIconClass = new HeaderIconClass();

            //For your testing
            protected function button1_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                imageID.source = headerIcon.source
            }

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                // TODO Auto-generated method stub
                headerIcon.source = "@Embed('/assets/images/Header.png')";
            }

        ]]>
    </fx:Script>

    <mx:VBox x="100" y="100">
        <s:Button label="Show Image" click="button1_clickHandler(event)"/>
        <s:Image id="imageID" width="50" height="50"/>
    </mx:VBox>
</s:Application>

如果您想使用它,可以将其保存在变量中: -

private var imagePath:String = xmlObject.child('icon').toString();

并将以下功能更改为: -

protected function application1_creationCompleteHandler(event:FlexEvent):void
{
    // TODO Auto-generated method stub
    headerIcon.source = "@Embed('"+imagePath+"')";
}

希望这可能有所帮助.....