【问题标题】:Dynamically create an embedded image动态创建嵌入图像
【发布时间】:2017-02-24 14:47:45
【问题描述】:

我需要动态创建一个带有图标的 LinkBut​​ton。文件名(图标)的格式为images/icon_0.pngimages/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*

【问题讨论】:

    标签: apache-flex


    【解决方案1】:

    抱歉,这行不通。

    由于imageindex是编译时变量,那么embedding标签会触发错误信息。

    为什么不覆盖按钮并添加额外的属性,如“iconPath”,它需要一个字符串路径而不是一个类对象。这样您就可以手动设置(在扩展按钮内)icon.source = iconPath 而无需使用嵌入。

    【讨论】:

      【解决方案2】:

      请试试这个。

      <?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>
      

      【讨论】:

      • 他想在嵌入标签内动态设置路径。对不起,但我没有看到你在使用它。也许是因为它不起作用......
      猜你喜欢
      • 2010-09-26
      • 1970-01-01
      • 2010-10-28
      • 2012-07-30
      • 2013-09-12
      • 2012-01-24
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      相关资源
      最近更新 更多