简体   繁体   English

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

[英]Adobe Air - Save window size and position

I'm trying to write a XML file to save the position and size of the app window in it. 我正在尝试编写一个XML文件来保存应用程序窗口的位置和大小。 I'm running into this error: 我遇到了这个错误:

TypeError: Error #1010: A term is undefined and has no properties. TypeError:错误#1010:术语未定义且没有属性。 MainTimeline/setupWindow() 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()

What is wrong with the code? 代码有什么问题?

It is call to setupWindow() that is causing the error, not the saveAppPosition method. 调用setupWindow()导致错误,而不是saveAppPosition方法。 It is executed as soon as your file is processed and nativeWindow is probably not yet ready. 一旦处理文件并且nativeWindow可能尚未就绪,它就会立即执行。

Move setupWindow() call into a method (FlexEvent.CREATION_COMPLETE handler for instance) and try again. setupWindow()调用移动到方法(例如FlexEvent.CREATION_COMPLETE处理程序)中,然后重试。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM