Using HTML formatting to output variables in Flex/MXML

眉间皱痕 提交于 2019-12-11 03:54:31

问题


I'm trying to output a sentence containing 4 variables, with their values emboldened using the following code:

<mx:Text width="100%" y="307">
    <mx:htmlText>
        <![CDATA[Showing data from <b>{labelStartTime.text} {labelStartDate.text}</b> to <b>{labelEndTime.text} {labelEndDate.text}</b>]]>
    </mx:htmlText>
</mx:Text>

However, this just outputs the variable names, rather than their values. I'm sure I'm missing something simple, but I'd appreciate any pointers.

Cheers.


回答1:


I know of a workaround:

Actionscript:

private var variable:String = "Variable String";
private var str:String = "<i><b>" + Variable + "</b></i>";

Mxml:

<Text htmlText="{str}" />



回答2:


I don't think it's possible to add bindings directly in a CDATA section like that, you do have a couple of options though :

  1. Use a function, taking advantage of BindingUtils.bindSetter :
    import mx.binding.utils.BindingUtils;
    //called on creationComplete
    private function init():void
    {
        BindingUtils.bindSetter(setHtmlText, labelStartTime, "text");
        BindingUtils.bindSetter(setHtmlText, labelStartDate, "text");
        BindingUtils.bindSetter(setHtmlText, labelEndTime, "text");
        BindingUtils.bindSetter(setHtmlText, labelEndDate, "text");
    }
    
    private function setHtmlText(val:String):void
    {
        myText.htmlText = "Showing data from <b>" + 
                            labelStartTime.text + " " + 
                            labelStartDate.text + "</b> to <b>" + 
                            labelEndTime.text + " " +
                            labelEndDate.text + "</b>";
    }
  2. Or simply encode the tags and insert them directly into the attribute :
    <mx:Text id="myText" width="100%" y="307" 
             htmlText="Showing data from &lt;b&gt;{labelStartTime.text} {labelStartDate.text}&lt;/b&gt; to &lt;b&gt;{labelEndTime.text} {labelEndDate.text}&lt;/b&gt;"/>
    This isn't really recommended as it makes the markup incredibly difficult to read, but you might get away with it for something small like this.


来源:https://stackoverflow.com/questions/424354/using-html-formatting-to-output-variables-in-flex-mxml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!