我的文件目录结构
我的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页面