从其他文件访问currentState?

时间:2011-05-14 00:00:05

标签: actionscript-3 flash flex flex4.5 flashbuilder4

注意:这不是this question的副本,因为答案对我没有帮助。

我有我的主文件(Main.mxml),我有一个主AS文件(main.as)。 Main.as由Main.mxml通过<fx:Script source="main.as"/>包含在内。在main.as中,我想更改Main.mxml的currentState。我该怎么做呢?

我已经尝试过的事情:

  • this.parent.currentState = "c_main";
  • this.parentDocument.currentState = "c_main";
  • this.parentApplication.currentState = "c_main";
  • this question中的答案。

3 个答案:

答案 0 :(得分:1)

这是你想要做的吗?


main.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"
                       addedToStage="_onStaged(event)"
                       stateChangeComplete="_stateChangeCompleteHandler(event)">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:states>
        <s:State name="c_main"/>
    </s:states>

    <fx:Script source="main.as" />
</s:WindowedApplication>

main.as:

import flash.events.Event;
import mx.events.FlexEvent;


// ActionScript file
private function _onStaged(event:Event = null):void
{
    this.currentState = "c_main";
}

protected function _stateChangeCompleteHandler(event:FlexEvent):void
{
    trace("the state was set to "+this.currentState);
}

答案 1 :(得分:0)

解决方案是将应用程序的currentState绑定到变量。请参阅下面的示例/概念验证(在Flex 4.6中测试和使用):

Main.as:

[Bindable]
public var curState:String = "c_main";

protected function swapState():void
{
if(curState=="c_main")
    curState="c_main2";
else
    curState="c_main";
} 

Main.mxml:

<?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"
           currentState="{curState}">   
  <s:states>
    <s:State name="c_main"/>
    <s:State name="c_main2"/>
  </s:states>

  <fx:Script source="Main.as"/>
  <s:VGroup>
 <s:Label text="1" includeIn="c_main" />
 <s:Label text="2" includeIn="c_main2" />
 <s:Button label="switch" click="swapState()"/>
  </s:VGroup>
</s:Application>

答案 2 :(得分:-1)

而不是

this.currentState = "c_main"; 

只需使用

currentState = "c_main";

它工作正常!