我的文件目錄結構
我的chatroom.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" minWidth="955" minHeight="600"
xmlns:hearain="hearain.*">
<fx:Declarations>
<!-- 將非可視元素(例如服務、值對象)放在此處 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
]]>
</fx:Script>
<mx:ViewStack id="room">
<s:NavigatorContent>
<hearain:login id="login">
</hearain:login>
</s:NavigatorContent>
<s:NavigatorContent>
<hearain:welcome id="welcome" width="300" height="200">
</hearain:welcome>
</s:NavigatorContent>
</mx:ViewStack>
</s:Application>
我的login.mxml組件
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="group1_creationCompleteHandler(event)"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.containers.ViewStack;
import mx.core.FlexGlobals;
import mx.events.FlexEvent;
private var vs:ViewStack; //vs是main.mxml中定義的viewstack
protected function group1_creationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
if(FlexGlobals.topLevelApplication is chatroom){
if(FlexGlobals.topLevelApplication.room is ViewStack){
vs = FlexGlobals.topLevelApplication.room as ViewStack;
}
}
}
// private function getChild(str:String):Object //這段代碼是爲了獲得指定頁面對應的對象
// {
// for each(var obj:Object in vs.getChildren()) //遍歷vs中的所有子組件(即所有頁面),頁面不會太多,頂多一二十個,所以這個方法不會太耗時
// {
// if(obj.name === str) //如果是想找的頁面,就返回這個對象。比如想找到welcome.mxml,則令str="welcome"
// {
// break;
// }
// }
// return obj;
// }
protected function button1_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
vs.selectedIndex = 1;//跳轉至welcomeu頁面
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 將非可視元素(例如服務、值對象)放在此處 -->
</fx:Declarations>
<s:Button x="200" y="121" label="登錄" click="button1_clickHandler(event)" />
</s:Group>
我的welcome.mxml組件
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Declarations>
<!-- 將非可視元素(例如服務、值對象)放在此處 -->
</fx:Declarations>
<s:Label id="w" text="歡迎窗口" />
</s:Group>
我的運行結果:
點擊登陸後跳轉至welcome頁面