我需要动态创建一个带图标的LinkButton。文件(图标)的名称格式为images/icon_0.png
,images/icon_1.png
,... images/icon_1000.png
。但我不知道这个按钮的具体图像。我只知道图标的索引。
我试过这个,没有成功:
var path:String = "@Embed(source='images/icon_" + imageindex + ".png')";
myButton.setStyle("icon", path);
我收到运行时错误:
Type Coercion failed:
*cannot convert "@Embed(source='images/icons/icon_427.png')" to Class*
答案 0 :(得分:1)
抱歉这不起作用。
由于imageindex是编译时变量,因此嵌入标记将触发错误消息。
为什么不覆盖按钮并添加额外的属性,比如'iconPath',它会期望一个字符串路径而不是一个Class对象。这样,您可以手动设置(在扩展按钮内)icon.source = iconPath,而无需使用嵌入。
答案 1 :(得分:0)
请试试这个。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" creationComplete="application1_creationCompleteHandler(event)"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
[Embed(source='icon_1.png')]
[Bindable]
private var linkButtonIcon:Class;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
lnkbutton.setStyle("icon",linkButtonIcon);
}
protected function button1_clickHandler(event:MouseEvent):void
{
[Embed(source='icon_2.png')]
var linkButtonIcon2:Class;
lnkbutton.setStyle("icon",linkButtonIcon2);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:VGroup>
<mx:LinkButton label="test" id="lnkbutton"/>
<s:Button label="change Icon" click="button1_clickHandler(event)"/>
</s:VGroup>
</s:Application>