如何通过单击 Flex4 中应用程序组件中的注销按钮返回主应用程序?

发布于 2024-11-28 01:56:12 字数 4193 浏览 1 评论 0原文

请检查代码并告诉我在单击应用程序组件中的注销后如何返回登录页面。

项目.mxml

<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" width="100%" height="100%" creationComplete="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <mx:StringValidator source="{uname}" id="unameValid" property="text"/>
        <mx:StringValidator source="{password}" id="passwordValid" property="text"/>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.ValidationResultEvent;
            import mx.validators.Validator;
            private var myValidators:Array = new Array;
            private var nav:Navigation;

            private function init():void{
                btnLogin.addEventListener(MouseEvent.CLICK,validateForm);
                myValidators = [unameValid,passwordValid];
            }

            private function validateForm(event:Event):void{
                //Alert.show("in validate form");
                var errors:Array = Validator.validateAll(myValidators);
                if(errors.length == 0){
                    loginUser();
                }else{
                    Alert.show("in else");
                }
            }

            private function loginUser():void{
                //Alert.show("In login Form");
                nav = new Navigation();
                this.addElement(nav);
            }
        ]]>
    </fx:Script>
    <s:Label text="Login Form" fontWeight="bold" fontSize="16" horizontalCenter="-110" 
             verticalAlign="middle" verticalCenter="-280"/>
    <mx:Form horizontalCenter="-120" verticalCenter="-200" fontWeight="bold" verticalGap="20">
        <mx:FormItem label="UserName">
            <s:TextInput id="uname" restrict="A-Z a-z" focusIn="uname.errorString = null" text="Priyanka"/>
        </mx:FormItem>
        <mx:FormItem label="Password">
            <s:TextInput id="password" displayAsPassword="true" focusIn="password.errorString = null" text="aamir#23"/>
        </mx:FormItem>
        <mx:FormItem>
            <mx:HBox horizontalGap="20">
                <s:Button label="Login" id="btnLogin" />
                <mx:LinkButton label="Register" id="register"/>
            </mx:HBox>
        </mx:FormItem>
    </mx:Form>
</s:Application>

导航.mxml

<mx:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="100%" height="100%" xmlns:local="*">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import spark.components.Application;
            private function logout(event:MouseEvent):void{

            }
        ]]>
    </fx:Script>
    <s:TabBar id="tabs" left="10" y="5" dataProvider="{vs}"/>
    <mx:ViewStack id="vs" width="90%" height="90%" left="10" y="30">
        <s:NavigatorContent label="DashBoard" width="100%" height="100%">
            <s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
                <s:Label text="in DashBoard"/>
            </s:BorderContainer>
        </s:NavigatorContent>
        <s:NavigatorContent label="User Information" width="100%" height="100%">
            <s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
                <s:Label text="in UserInfo"/>
            </s:BorderContainer>
        </s:NavigatorContent>
    </mx:ViewStack>
    <s:Button x="494" y="1" label="Logout" id="btnLogout" click="logout(event);"/>
</mx:Panel>

Please check the code and tell me How I will be back in Login Page after click of logout which is in application's component.

Project.mxml

<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" width="100%" height="100%" creationComplete="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <mx:StringValidator source="{uname}" id="unameValid" property="text"/>
        <mx:StringValidator source="{password}" id="passwordValid" property="text"/>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.ValidationResultEvent;
            import mx.validators.Validator;
            private var myValidators:Array = new Array;
            private var nav:Navigation;

            private function init():void{
                btnLogin.addEventListener(MouseEvent.CLICK,validateForm);
                myValidators = [unameValid,passwordValid];
            }

            private function validateForm(event:Event):void{
                //Alert.show("in validate form");
                var errors:Array = Validator.validateAll(myValidators);
                if(errors.length == 0){
                    loginUser();
                }else{
                    Alert.show("in else");
                }
            }

            private function loginUser():void{
                //Alert.show("In login Form");
                nav = new Navigation();
                this.addElement(nav);
            }
        ]]>
    </fx:Script>
    <s:Label text="Login Form" fontWeight="bold" fontSize="16" horizontalCenter="-110" 
             verticalAlign="middle" verticalCenter="-280"/>
    <mx:Form horizontalCenter="-120" verticalCenter="-200" fontWeight="bold" verticalGap="20">
        <mx:FormItem label="UserName">
            <s:TextInput id="uname" restrict="A-Z a-z" focusIn="uname.errorString = null" text="Priyanka"/>
        </mx:FormItem>
        <mx:FormItem label="Password">
            <s:TextInput id="password" displayAsPassword="true" focusIn="password.errorString = null" text="aamir#23"/>
        </mx:FormItem>
        <mx:FormItem>
            <mx:HBox horizontalGap="20">
                <s:Button label="Login" id="btnLogin" />
                <mx:LinkButton label="Register" id="register"/>
            </mx:HBox>
        </mx:FormItem>
    </mx:Form>
</s:Application>

Navigation.mxml

<mx:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="100%" height="100%" xmlns:local="*">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import spark.components.Application;
            private function logout(event:MouseEvent):void{

            }
        ]]>
    </fx:Script>
    <s:TabBar id="tabs" left="10" y="5" dataProvider="{vs}"/>
    <mx:ViewStack id="vs" width="90%" height="90%" left="10" y="30">
        <s:NavigatorContent label="DashBoard" width="100%" height="100%">
            <s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
                <s:Label text="in DashBoard"/>
            </s:BorderContainer>
        </s:NavigatorContent>
        <s:NavigatorContent label="User Information" width="100%" height="100%">
            <s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
                <s:Label text="in UserInfo"/>
            </s:BorderContainer>
        </s:NavigatorContent>
    </mx:ViewStack>
    <s:Button x="494" y="1" label="Logout" id="btnLogout" click="logout(event);"/>
</mx:Panel>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痕至 2024-12-05 01:56:13

您应该创建一个自定义事件:

package 
{
    import flash.events.Event;

    public class LogoutEvent extends Event
    {
        public static const LOG_OUT:String = "logOut";

        public function LogoutEvent(type:String)
        {
            super(type,false,false);
        }

        public override function clone():Event
        {
            return new LogoutEvent(type);
        }

        public override function toString():String
        {
            return formatToString("LogoutEvent");
        }
    }
}

现在您应该分派此事件:

<mx:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="100%" height="100%" xmlns:local="*">
    <fx:Metadata>
        [Event(name="logOut", type="LogoutEvent")]
    </fx:Metadata>
    <fx:Script>
    <![CDATA[
        import spark.components.Application;
        private function logout(event:MouseEvent):void{
            dispatchEvent(new LogoutEvent(LogoutEvent.LOG_OUT));
        }
    ]]>
    </fx:Script>
    <s:TabBar id="tabs" left="10" y="5" dataProvider="{vs}"/>
    <mx:ViewStack id="vs" width="90%" height="90%" left="10" y="30">
        <s:NavigatorContent label="DashBoard" width="100%" height="100%">
            <s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
                <s:Label text="in DashBoard"/>
            </s:BorderContainer>
        </s:NavigatorContent>
        <s:NavigatorContent label="User Information" width="100%" height="100%">
            <s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
                <s:Label text="in UserInfo"/>
            </s:BorderContainer>
        </s:NavigatorContent>
    </mx:ViewStack>
    <s:Button x="494" y="1" label="Logout" id="btnLogout" click="logout(event);"/>
</mx:Panel>

最后,您应该处理它并关闭窗口:

<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" width="100%" height="100%" creationComplete="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <mx:StringValidator source="{uname}" id="unameValid" property="text"/>
        <mx:StringValidator source="{password}" id="passwordValid" property="text"/>
    </fx:Declarations>
    <fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.ValidationResultEvent;
        import mx.validators.Validator;
        private var myValidators:Array = new Array;
        private var nav:Navigation;

        private function init():void{
            btnLogin.addEventListener(MouseEvent.CLICK,validateForm);
            myValidators = [unameValid,passwordValid];
        }

        private function validateForm(event:Event):void{
            //Alert.show("in validate form");
            var errors:Array = Validator.validateAll(myValidators);
            if(errors.length == 0){
                loginUser();
            }else{
                Alert.show("in else");
            }
        }

        private function loginUser():void{
            //Alert.show("In login Form");
            nav = new Navigation();
            this.addElement(nav);
            nav.addEventListener(LogoutEvent.LOG_OUT, nav_logOutHandler);
        }

        private function nav_logOutHandler(event:LogoutEvent):void
        {
            removeElement(nav);
            nav.removeEventListener(LogoutEvent.LOG_OUT, nav_logOutHandler);
            nav = null;
        }
    ]]>
    </fx:Script>
    <s:Label text="Login Form" fontWeight="bold" fontSize="16" horizontalCenter="-110" 
        verticalAlign="middle" verticalCenter="-280"/>
    <mx:Form horizontalCenter="-120" verticalCenter="-200" fontWeight="bold" verticalGap="20">
        <mx:FormItem label="UserName">
            <s:TextInput id="uname" restrict="A-Z a-z" focusIn="uname.errorString = null" text="Priyanka"/>
        </mx:FormItem>
        <mx:FormItem label="Password">
            <s:TextInput id="password" displayAsPassword="true" focusIn="password.errorString = null" text="aamir#23"/>
        </mx:FormItem>
        <mx:FormItem>
            <mx:HBox horizontalGap="20">
                <s:Button label="Login" id="btnLogin" />
                <mx:LinkButton label="Register" id="register"/>
            </mx:HBox>
        </mx:FormItem>
    </mx:Form>
</s:Application>

You should create a custom event:

package 
{
    import flash.events.Event;

    public class LogoutEvent extends Event
    {
        public static const LOG_OUT:String = "logOut";

        public function LogoutEvent(type:String)
        {
            super(type,false,false);
        }

        public override function clone():Event
        {
            return new LogoutEvent(type);
        }

        public override function toString():String
        {
            return formatToString("LogoutEvent");
        }
    }
}

Now you should dispatch this event:

<mx:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="100%" height="100%" xmlns:local="*">
    <fx:Metadata>
        [Event(name="logOut", type="LogoutEvent")]
    </fx:Metadata>
    <fx:Script>
    <![CDATA[
        import spark.components.Application;
        private function logout(event:MouseEvent):void{
            dispatchEvent(new LogoutEvent(LogoutEvent.LOG_OUT));
        }
    ]]>
    </fx:Script>
    <s:TabBar id="tabs" left="10" y="5" dataProvider="{vs}"/>
    <mx:ViewStack id="vs" width="90%" height="90%" left="10" y="30">
        <s:NavigatorContent label="DashBoard" width="100%" height="100%">
            <s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
                <s:Label text="in DashBoard"/>
            </s:BorderContainer>
        </s:NavigatorContent>
        <s:NavigatorContent label="User Information" width="100%" height="100%">
            <s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
                <s:Label text="in UserInfo"/>
            </s:BorderContainer>
        </s:NavigatorContent>
    </mx:ViewStack>
    <s:Button x="494" y="1" label="Logout" id="btnLogout" click="logout(event);"/>
</mx:Panel>

Finally, you should handle it and close your window:

<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" width="100%" height="100%" creationComplete="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <mx:StringValidator source="{uname}" id="unameValid" property="text"/>
        <mx:StringValidator source="{password}" id="passwordValid" property="text"/>
    </fx:Declarations>
    <fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.ValidationResultEvent;
        import mx.validators.Validator;
        private var myValidators:Array = new Array;
        private var nav:Navigation;

        private function init():void{
            btnLogin.addEventListener(MouseEvent.CLICK,validateForm);
            myValidators = [unameValid,passwordValid];
        }

        private function validateForm(event:Event):void{
            //Alert.show("in validate form");
            var errors:Array = Validator.validateAll(myValidators);
            if(errors.length == 0){
                loginUser();
            }else{
                Alert.show("in else");
            }
        }

        private function loginUser():void{
            //Alert.show("In login Form");
            nav = new Navigation();
            this.addElement(nav);
            nav.addEventListener(LogoutEvent.LOG_OUT, nav_logOutHandler);
        }

        private function nav_logOutHandler(event:LogoutEvent):void
        {
            removeElement(nav);
            nav.removeEventListener(LogoutEvent.LOG_OUT, nav_logOutHandler);
            nav = null;
        }
    ]]>
    </fx:Script>
    <s:Label text="Login Form" fontWeight="bold" fontSize="16" horizontalCenter="-110" 
        verticalAlign="middle" verticalCenter="-280"/>
    <mx:Form horizontalCenter="-120" verticalCenter="-200" fontWeight="bold" verticalGap="20">
        <mx:FormItem label="UserName">
            <s:TextInput id="uname" restrict="A-Z a-z" focusIn="uname.errorString = null" text="Priyanka"/>
        </mx:FormItem>
        <mx:FormItem label="Password">
            <s:TextInput id="password" displayAsPassword="true" focusIn="password.errorString = null" text="aamir#23"/>
        </mx:FormItem>
        <mx:FormItem>
            <mx:HBox horizontalGap="20">
                <s:Button label="Login" id="btnLogin" />
                <mx:LinkButton label="Register" id="register"/>
            </mx:HBox>
        </mx:FormItem>
    </mx:Form>
</s:Application>
暮年慕年 2024-12-05 01:56:13

最快的方法是:

private function logout(event:MouseEvent):void{
    parent.removeElement(this);
}

然而,康斯坦丁的方法是正确的方法。


另外,如果您使用的是 Flex 4,为什么不使用 Spark FormFormItemPanelHGroup (而不是 HBox)

The quickest way would be:

private function logout(event:MouseEvent):void{
    parent.removeElement(this);
}

However, Constantiner's method is the proper way.


Also, if you are using flex 4, why not use the spark Form,FormItem,Panel and HGroup (Instead of HBox)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文