Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache flex 如何使用getStyle在Flex中用常量引用替换字符串?_Apache Flex_Mxml - Fatal编程技术网

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

Apache flex 如何使用getStyle在Flex中用常量引用替换字符串?,apache-flex,mxml,Apache Flex,Mxml,我想用替换mxml中的getStyle('someStyle') private static const SOMESTYLE:String = "someStyle"; [..] <mx:Image source="{getStyle(SOMESTYLE)}" /> private static const SOMESTYLE:String=“SOMESTYLE”; [..] 其中someStyle在我的CSS中定义 这会编译,但在运行时出错:在package.class上找不

我想用替换mxml中的
getStyle('someStyle')

private static const SOMESTYLE:String = "someStyle";
[..]
<mx:Image source="{getStyle(SOMESTYLE)}" />
private static const SOMESTYLE:String=“SOMESTYLE”;
[..]
其中someStyle在我的CSS中定义

这会编译,但在运行时出错:在package.class上找不到error#1069:Property SOMESTYLE,并且没有默认值

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


编辑:我正在使用Flex4.6。这在Flex3.5中运行良好。

类中存在静态变量或方法;不在类的实例上。因此,您必须使用类名引用它们;不是实例名。您没有在代码中指定类名

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


下面是一些代码,显示如何在定义静态常量的同一类中访问该常量。此文件名为StaticVariablesSameClass\u,因此:

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

@命名空间s“library://ns.adobe.com/flex/spark";
@名称空间mx“library://ns.adobe.com/flex/mx";
s |应用{
someStyle:'https://www.flextras.com/Assets/images/flextras_logo3.gif';
}

回答得很好!我只想补充一点,常量应该是public实际上很有意义,但是,我在编译:[module]SetupUtil.as:Access of undefined property ClassName时遇到了一个错误。发生这种情况是因为我想在与mxml相同的类中定义常量吗?您必须用类的实际名称替换ClassName。正如@Timofedavydik所说;我要确保静态变量是公共的;不只是为了说清楚;当我的常量与我的mxml在同一个类中时,这不起作用,它导致了我上面描述的错误。然而,将常数移动到另一个类中就成功了。我猜这与静态和非静态上下文有关。是的,我公开了常量,并用正确的名称交换了类名。我添加了一个代码示例,演示如何在定义静态变量的同一个类中访问静态变量@Timofedavydik我想它不必公开。