Flex 3: How to change state in code, using a variable
How do I change the application state in code, using a variable? when I provide a static string it works fine, but not with a variable.
For example, this works: (parent as mx.core.Application).currentState= 'history'
And this does not: (parent as mx.core.Application).currentState= @data
Yes, I know that @data is being populated, as I print it in an alert box. And yes, I have already tried "currentState = @data" and "currentState = '@data'. Sadly, they did not work.
Here is the menu object:
<mx:MenuBar id="m开发者_StackOverflow中文版nuMain" labelField="@label" itemClick="menuHandler(event);">
<mx:XMLList>
<menuitem label="File">
<menuitem label="Exit" data="exit" />
</menuitem>
<menuitem label="View">
<menuitem label="Home" data="home" />
<menuitem label="Monitor" data="impmon" />
<menuitem label="History" data="history" />
<menuitem label="Tables" data="tables" />
<menuitem label="Schema View" data="schema" />
</menuitem>
</mx:XMLList>
</mx:MenuBar>
Here is the handler:
private function menuHandler(event:MenuEvent):void {
var newstate:String;
newstate = @data.toString();
(parent as mx.core.Application).currentState = newstate;
}
Here's a quick example I whipped up:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:states>
<mx:State name="exit">
<mx:SetProperty target="{label1}" name="text" value="Exit State"/>
</mx:State>
<mx:State name="home">
<mx:SetProperty target="{label1}" name="text" value="Home State"/>
</mx:State>
<mx:State name="impmon">
<mx:SetProperty target="{label1}" name="text" value="Impmon State"/>
</mx:State>
<mx:State name="history">
<mx:SetProperty target="{label1}" name="text" value="History State"/>
</mx:State>
<mx:State name="tables">
<mx:SetProperty target="{label1}" name="text" value="Tables State"/>
</mx:State>
<mx:State name="schema">
<mx:SetProperty target="{label1}" name="text" value="Schema State"/>
</mx:State>
</mx:states>
<mx:Script>
<![CDATA[
import flash.utils.getQualifiedClassName;
import mx.events.MenuEvent;
private function menuHandler(event:MenuEvent):void
{
trace("Clicked", String(event.item.@data));
currentState = String(event.item.@data);
}
]]>
</mx:Script>
<mx:MenuBar id="mnuMain" labelField="@label" itemClick="menuHandler(event)">
<mx:XMLList>
<menuitem label="File">
<menuitem label="Exit" data="exit" />
</menuitem>
<menuitem label="View">
<menuitem label="Home" data="home" />
<menuitem label="Monitor" data="impmon" />
<menuitem label="History" data="history" />
<menuitem label="Tables" data="tables" />
<menuitem label="Schema View" data="schema" />
</menuitem>
</mx:XMLList>
</mx:MenuBar>
<mx:Label horizontalCenter="0" verticalCenter="0" fontSize="30" fontWeight="bold" color="#ffffff" text="Default State" id="label1">
<mx:filters>
<mx:DropShadowFilter/>
</mx:filters>
</mx:Label>
</mx:Application>
I am guessing you are getting this from an XML, try xml.@data.toString()
Hope that helps.
EDIT: The mxml should look something like the below and it should work fine with event.item.@data
<mx:MenuBar id="mnuMain" labelField="@label" itemClick="menuHandler(event);" showRoot="false">
<mx:dataProvider>
<mx:XMLList>
<menu>
<menuitem label="test">
<menuitem label="History" data="history" />
</menuitem>
</menu>
</mx:XMLList>
</mx:dataProvider>
</mx:MenuBar>
精彩评论