Adobe Air - 保存窗口大小和位置

时间:2012-03-07 16:04:23

标签: actionscript-3 flex air

我正在尝试编写一个XML文件来保存应用程序窗口的位置和大小。我遇到了这个错误:

  

TypeError:错误#1010:术语未定义且没有属性。 MainTimeline / setupWindow()

AS

import flash.display.NativeWindowInitOptions;
import flash.display.NativeWindowSystemChrome;
import flash.display.NativeWindowType;
import flash.display.NativeWindow;


function setupWindow(e:Event = null):void
{
    gotoLastPosition();
    this.nativeWindow.addEventListener( Event.CLOSING, saveAppPosition );
}

function saveAppPosition(e:Event = null):void
{
    var xml:XML = new XML('<position x="' + this.nativeWindow.x + '" y="' + this.nativeWindow.y + '" width="' + this.width + '" height="' + this.height + '"/>');

    var f:File = File.applicationStorageDirectory.resolvePath("appPosition.xml");
    var s:FileStream = new FileStream();
    try
    {
        s.open(f,flash.filesystem.FileMode.WRITE);
        s.writeUTFBytes(xml.toXMLString());
    }
    catch (e:Error)
    {
        //trace(error( e ));
    }
    finally
    {
        s.close();
    }
}

function gotoLastPosition():void
{
    var f:File = File.applicationStorageDirectory.resolvePath("appPosition.xml");
    if (f.exists)
    {
        var s:FileStream = new FileStream();
        try
        {
            s.open(f,flash.filesystem.FileMode.READ);
            var xml:XML = XML(s.readUTFBytes(s.bytesAvailable));

            this.nativeWindow.x = xml. @ x;
            this.nativeWindow.y = xml. @ y;
            this.width = xml. @ width;
            this.height = xml. @ height;
        }
        finally
        {
            s.close();
        }
    }
}


setupWindow()

代码有什么问题?

1 个答案:

答案 0 :(得分:4)

调用setupWindow()导致错误,而不是 saveAppPosition 方法。它会在您的文件处理完毕后立即执行, nativeWindow 可能尚未就绪。

setupWindow()调用移动到方法(例如FlexEvent.CREATION_COMPLETE处理程序)中,然后重试。

希望有所帮助。