在windowedapplication flex中导航

时间:2013-06-24 10:25:17

标签: actionscript-3 flex air desktop-application

我正在开发flex的桌面空中应用程序。我有2个mxml (一个mxml是窗口应用程序,另一个是mxml组)。我想从一个mxml文件(login.mxml)导航到另一个(nextpage.mxml)。

我怎样才能实现这个目标?

login.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">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>`enter code here`
<![CDATA[
public function onLogin()
{
// What code should i use to navigate to another mxml page i.e nextpage.mxml
}
]]>
</fx:Script><s:Button id="btn" name="Login" click="onLogin()"/</s:WindowedApplication>

nextpage.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">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label text="Login Success/></s:Group>

1 个答案:

答案 0 :(得分:0)

将主应用程序实现为应用程序状态的协调者将是一件好事。

在这种情况下,您可以使用ViewStack组件,它具有Login和NextPage组件作为子组件。组件通过事件与应用程序通信。根据当前事件,状态会发生变化。

//主要应用

<?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" xmlns:fld01="com.fld01.*">

<mx:ViewStack id="vsMain">
    <s:NavigatorContent id="ncLogin">
        <fld01:Login id="grLogin" evtLogin="{vsMain.selectedChild = ncNextPage}"/>
    </s:NavigatorContent>

    <s:NavigatorContent id="ncNextPage">
        <fld01:Nextpage id="grNextPage" evtLogout="{vsMain.selectedChild = ncLogin}"/>
    </s:NavigatorContent>
</mx:ViewStack>

</s:WindowedApplication>

//登录

<?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">

<fx:Metadata>
    [Event(name="evtLogin", type="flash.events.Event")]
</fx:Metadata>

<fx:Script>
    <![CDATA[
        public function onLogin():void
        {
            this.dispatchEvent(new Event("evtLogin"));  
        }
    ]]>
</fx:Script>

<s:Button id="btn" x="40" y="50" label="Login" click="onLogin()"/>
</s:Group>

//下一页

<?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">

<fx:Metadata>
    [Event(name="evtLogout", type="flash.events.Event")]
</fx:Metadata>

<fx:Script>
    <![CDATA[
        public function onLogout():void
        {
            this.dispatchEvent(new Event("evtLogout")); 
        }
    ]]>
</fx:Script>

<s:Label x="40" y="50" text="Login Success"/>

<s:Button id="btn" x="40" y="80" label="Logout" click="onLogout()"/>

</s:Group>