Insert an image using a String variable that contains the file name

I am trying to embed images in mx: tree:

<mx:Tree labelField="name" id="tree" 
    folderOpenIcon="@Embed(source='assets/images/test.png')"
    folderClosedIcon="@Embed(source='assets/images/test.png')"
    defaultLeafIcon="@Embed(source='assets/images/test.png')">
</mx:Tree>

This works fine, but I will embed images with a String variable.

I have a variable and a function

[Bindable]
private var folderIcon:String;

public function setIcon(icon:String):void {
    folderIcon = icon; // "assets/images/test.png"
}

But how can I replace these lines

folderOpenIcon="@Embed(source='assets/images/test.png')"
folderClosedIcon="@Embed(source='assets/images/test.png')"
defaultLeafIcon="@Embed(source='assets/images/test.png')"

with

folderIcon

? Does anyone know this? Or should / can I use style sheets?

Many thanks in advance and best regards.

+3
source share
3 answers

I'm not sure why you need this. In any case, if you are trying to embed images in AS3, you should do the following

class MyClass{

[Embed(source='assets/images/test_open.png')]
private static var folderOpenIcon:Class;

[Embed(source='assets/images/test_close.png')]
private static var folderClosedIcon:Class;

[Embed(source='assets/images/test_default.png')]
private static var defaultLeafIcon:Class;

[Bindable]
private var fodlerIcon:Class 

public function setIcon(iconClass:Class):void {
    folderIcon = iconClass:Class;
}

private function testIcon():void{
    setIcon(defaultLeafIcon); 
    // or
   setIcon(folderOpenIcon); 
    // etc

}    
}
+2
source

Nesting occurs at compile time; not at runtime. You cannot embed an element using the set method.

, PNG Flex:

http://livedocs.adobe.com/flex/3/html/help.html?content=embed_4.html

    [Embed(source="assets/images/test.png")]
    [Bindable]
    public var folderIcon:Class;

:

folderOpenIcon="{folderIcon}"
folderClosedIcon="{folderIcon}"
defaultLeafIcon="{folderIcon}"
+1

I can do this with style sheets. You can add

styleName="myButton"

and through the styles:

<mx:Style>
    .projectButton {
        icon: Embed("assets/images/test.png");
    }
</mx:Style>

(an external style sheet is also possible).

0
source

Source: https://habr.com/ru/post/1751922/


All Articles