在关闭AIR程序窗口前显示Alert

AIRClose.mxml

 1<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
 2                        layout="vertical" showStatusBar="false"
 3                        closing="closeHandler(event)">
 4    <mx:Script>
 5        <![CDATA[
 6            import org.zengrong.utils.Dialog;
 7            private function closeHandler(evt:Event):void
 8            {
 9                trace(evt.toString());
10                evt.preventDefault();
11                Dialog.confirm('确定退出?', _close);
12            }
13            
14            private function _close($yes:Boolean):void
15            {
16                if($yes) this.nativeApplication.exit();
17            }
18
19        ]]>
20    </mx:Script>    
21</mx:WindowedApplication>

有关Dialog的用法与最新源码,详见zrong's as3lib(下附源码)

Dialog.as

 1package org.zengrong.utils
 2{
 3    import flash.display.Sprite;
 4    
 5    import mx.controls.Alert;
 6    import mx.core.Application;
 7    import mx.events.CloseEvent;
 8    
 9    public class Dialog
10    {
11        public static function alert($info:String, $title:String=''):void
12        {
13            Alert.show($info, $title, 4, Application.application as Sprite);
14        }
15        
16        /**
17        * 弹出confirm确认对话框,根据用户的交互返回是否确认布尔值
18        * @param $s 要显示的信息
19        * @param $closeFun 关闭确认对话框时调用的函数
20        */
21        public static function confirm($s:String, $closeFun:Function):void
22        {
23            var __fun:Function = function(evt:CloseEvent):void
24            {
25                $closeFun(evt.detail == Alert.YES);
26            }
27            Alert.show($s, '', Alert.YES|Alert.NO, Application.application as Sprite, __fun);
28        }
29    }
30}