1
votes

I want to replace getStyle('someStyle') in mxml with

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

where someStyle is defined in my CSS.

This compiles, but gives me error on runtime: Error #1069: Property SOMESTYLE not found on package.class and there is no default value.

What is the appropriate way to reference to the class constant from the mxml?

edit: I'm using Flex 4.6. This worked just fine in Flex 3.5.

1

1 Answers

3
votes

Static variables or methods exist on the class; not on an instance of the class. So you have to reference them using the class name; not the instance name. You did not specify the class name in your code.

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

Here is some code that shows accessing a static constant inside the same class that defines it. This file is named 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>