0

我必须使用这样的东西创建图像对象

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


但现在 embedd 中的字符串是从我xmlObject.child("icon");尝试过 的一些 xml 动态带来的

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

但是它会给出错误,例如

无效元数据

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

4

2 回答 2

1

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

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

于 2012-04-25T14:01:26.203 回答
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+"')";
}

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

于 2012-04-26T06:27:46.703 回答