如何在AIR htmlView.stage.nativeWindow中检测用户关闭?

时间:2013-01-05 17:43:03

标签: flex air

我正在编写一个通用的OAuth组件(是的,它将是开源),我想检测用户是否已点击htmlView上的关闭小部件(或更确切地说,是其浏览器窗口)

我尝试在htmlView和html.stage.nativeWindow上放置CLOSE侦听器无济于事。

提前致谢

1 个答案:

答案 0 :(得分:0)

此代码可能会有所帮助

package com.bigspaceship.core.air
{
    // flash
    import flash.display.NativeWindow;
    import flash.desktop.NativeApplication;    

    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class NativeWindowManager extends EventDispatcher
    {
        private var _window            :NativeWindow;
        private var _isMainWindow    :Boolean;
        public var isQuitBlocked    :Boolean;

        public static const PROMPT    :String        = "windowQuitPrompt";

        public function NativeWindowManager($window:NativeWindow,$isMainWindow:Boolean = false):void
        {
            _window = $window;
            _isMainWindow = $isMainWindow;
            _window.addEventListener(Event.CLOSING,_windowOnClose,false,0,true);
        };

        public function get window():NativeWindow { return _window; };

        private function _mainWindowOnClose($evt:Event):void
        {
            $evt.preventDefault();

            if(isQuitBlocked) dispatchEvent(new Event(PROMPT));
            else close();
        };

        public function close():void
        {
            _window.removeEventListener(Event.CLOSING,_windowOnClose);

            if(_isMainWindow) NativeApplication.nativeApplication.exit();
            else _window.close();

            _window = null;
        };
    };
};