如何使用getStyle在Flex中用常量引用替换字符串?

时间:2012-10-17 12:08:02

标签: flex mxml

我想用

替换mxml中的getStyle('someStyle')
private static const SOMESTYLE:String = "someStyle";
[..]
<mx:Image source="{getStyle(SOMESTYLE)}" />

其中someStyle在我的CSS中定义。

这编译,但在运行时给出错误:错误#1069:在package.class上找不到属性SOMESTYLE,并且没有默认值。

从mxml引用类常量的适当方法是什么?

编辑:我正在使用Flex 4.6。这在Flex 3.5中运行得很好。

1 个答案:

答案 0 :(得分:3)

类上存在静态变量或方法;不是在班级的实例上。所以你必须使用类名引用它们;不是实例名称。您没有在代码中指定类名。

<mx:Image source="{getStyle(ClassName.SOMESTYLE)}" />

以下是一些代码,显示了在定义它的同一个类中访问静态常量。该文件名为StaticVariablesInSameClass_SO:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";


        s|Application{
            someStyle : 'https://www.flextras.com/Assets/images/flextras_logo3.gif';
        }
    </fx:Style>

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            private static const SOMESTYLE:String = "someStyle";

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                trace(this.getStyle('someStyle'));
                trace(this.getStyle(StaticVariablesInSameClass_SO.SOMESTYLE));
            }

        ]]>
    </fx:Script>

</s:Application>
相关问题