简体   繁体   English

Flex嵌入图像,其中路径基于绑定变量

[英]Flex embedding an image where the path is based on a bound variable

I am trying to embed an image using an item renderer in my Flex project. 我试图在我的Flex项目中使用项目渲染器嵌入图像。

The image path however is a String passed in as a bound variable. 但是,图像路径是作为绑定变量传入的String。

I am aware that 我知道

<s:BitmapImage source="@Embed('/../assets/image.png')" />

works because the image is embedded at runtime? 有效,因为图像是在运行时嵌入的? (Could someone please clarify this) (有人可以澄清一下)

How would i go about embedding my bound string, somewhat like this: 我将如何嵌入绑定的字符串,有点像这样:

<s:BitmapImage source="@Embed('/../assets/{data.image}')" />

Many Thanks 非常感谢

I think a better choice if you'd like to embed the image but find it dynamically at runtime is: Embed all of the images it could be and then grab a reference to it dynamically. 我认为,如果您想嵌入图像但在运行时动态找到它,一个更好的选择是:嵌入所有可能的图像,然后动态获取对其的引用。 We generally use a pattern like this: 我们通常使用这样的模式:

public class Icons {
    [Embed(source="icons/icon1.png")]
    public var icon1:Class;
    [Embed(source="icons/icon2.png")]
    public var icon2:Class;
}

Then you can dynamically grab the embedded images from your Icons instance at run-time. 然后,您可以在运行时从Icons实例动态获取嵌入式图像。

Edit - self contained example - I'll use an item renderer since I think that's what you're doing. 编辑-包含示例 -我将使用项目渲染器,因为我认为这就是您正在做的事情。 Let's assume data.image can be 'plane' 'train' or 'automobile' 假设data.image可以是“飞机”,“火车”或“汽车”

<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" 
            xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script>
        <![CDATA[

            [Embed(source="/assets/icons/plane.png")]
            public var plane : Class;
            [Embed(source="/assets/icons/train.png")]
            public var train : Class;
            [Embed(source="/assets/icons/automobile.png")]
            public var automobile : Class;
        ]]>
    </fx:Script>

    <s:Image source="{this[data.image]}"/>

</s:ItemRenderer>

This is a really simple example and not the BEST way to implement, but you get the idea. 这是一个非常简单的示例,而不是最佳实现方式,但是您可以理解。

I like embed icons with css files. 我喜欢在CSS文件中嵌入图标。 Then in ItemRenderer you can set css class and get image which you want. 然后,在ItemRenderer中,您可以设置CSS类并获取所需的图像。

css file or mxml css block: css文件或mxml css块:

.icons 
{
    bender: Embed(source="/assets/bender.png");
    /* other icons */
}

In renderer, when you override set data method: 在渲染器中,当您覆盖设置数据方法时:

override public function set data(value:Object):void
{
    super.data = value;

    var iconName:String = data.image;

    if ( iconName )
    {
        var cssDecl2:CSSStyleDeclaration = styleManager.getStyleDeclaration(".icons");
        var IconClass:Class = cssDecl2.getStyle( iconName );
        bmImage.source = new IconClass();
    }
}

and bmImage as id s:BitmapImage: 和bmImage作为id s:BitmapImage:

<s:BitmapImage id="bmImage" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM