简体   繁体   中英

Flex 4: State Change Event

我可以使用Flex 4中的任何事件来检测状态变化吗?

I know this question is old but by googling for state change events I still get here so for people that want to know:

There is a StateChangeEvent.CURRENT_STATE_CHANGE event that is dispatched by the component, so your application can also listen for that.

In your listener function you can then acces the StateChangeEvent.oldState and StateChangeEvent.newState properties.

If you are talking about view states the answer is yes, you can listen for the enterState event like this (sorry for the simplicity of the example, it's part of a project I'm working on and I removed any relevant parts of the code):

<?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="800" minHeight="600"
           currentState="loading">

<fx:Script>
    <![CDATA[
        import mx.controls.Alert;

        private function onEnterLoadingState():void{
            Alert.show("Enter the loading state.", "Application");
        }

        private function onEnterLoginState():void{
            Alert.show("Enter the login state.", "Application");
        }

        private function onEnterAddState():void{
            Alert.show("Enter the addUser state.", "Application");
        }

        private function changeState(state:String):void{
            currentState = state;
        }
    ]]>
</fx:Script>

<s:states>
    <s:State name="loading" enterState="onEnterLoadingState()"/>
    <s:State name="login" enterState="onEnterLoginState()"/>
    <s:State name="addUser" enterState="onEnterAddState()"/>
</s:states>

<s:Panel id="loadView" includeIn="loading" title="Loading">
    <s:Button label="Go to login" click="changeState('login')"/>
</s:Panel>
<s:Panel id="loginView" includeIn="login" title="Login">
    <s:Button label="Go to addUser" click="changeState('addUser')"/>
</s:Panel>
<s:Panel id="addView" includeIn="addUser" title="AddUser">
    <s:Button label="Return to loading" click="changeState('loading')"/>
</s:Panel>
</s:Application>

And there is an exitState event in case you need it. I hope this helps you.

There are multiple state events you can listen for on any UIComponent class:

  • FlexEvent.STATE_CHANGE_COMPLETE
  • FlexEvent.STATE_CHANGE_INTERRUPTED
  • StateChangeEvent.CURRENT_STATE_CHANGING
  • StateChangeEvent.CURRENT_STATE_CHANGE
  • FlexEvent.ENTER_STATE
  • FlexEvent.EXIT_STATE

MXML:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"

                       enterState="windowedapplication1_enterStateHandler(event)"
                       exitState="windowedapplication1_exitStateHandler(event)"
                       currentStateChange="windowedapplication1_currentStateChangeHandler(event)"
                       currentStateChanging="windowedapplication1_currentStateChangingHandler(event)"
                       stateChangeInterrupted="windowedapplication1_stateChangeInterruptedHandler(event)"
                       stateChangeComplete="windowedapplication1_stateChangeCompleteHandler(event)">

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;


            protected function windowedapplication1_stateChangeCompleteHandler(event:FlexEvent):void
            {

            }

            protected function windowedapplication1_stateChangeInterruptedHandler(event:FlexEvent):void
            {

            }

            protected function windowedapplication1_currentStateChangeHandler(event:StateChangeEvent):void
            {
                var oldState:String = event.oldState;
                var newState:String = event.newState;
            }

            protected function windowedapplication1_currentStateChangingHandler(event:StateChangeEvent):void
            {
                var oldState:String = event.oldState;
                var newState:String = event.newState;
            }

            protected function windowedapplication1_enterStateHandler(event:FlexEvent):void
            {

            }

            protected function windowedapplication1_exitStateHandler(event:FlexEvent):void
            {

            }
        ]]>
    </fx:Script>
</s:WindowedApplication>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM