AIR: save application location and size

泄露秘密 提交于 2019-12-09 14:07:24

问题


OK I am trying to make it so that when a user launches my air app, it positions itself and sizes itself to whatever it was the last time it was opened.

Here is my code:

private function init():void
{
    gotoLastPosition();
    this.nativeWindow.addEventListener( Event.CLOSING, saveAppPosition );
}

private 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());

        s.close();
    }
    catch(e:Error){}
}

private function gotoLastPosition():void
{
    var f:File = File.applicationStorageDirectory.resolvePath("appPosition.xml");   
    if(f.exists)
    {
        var s:FileStream = new FileStream();
        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;
    }
}

This does work, however, I am getting complaints from users that sometimes when they launch the app, it is no where to be found (off screen), they can right click the taskbar item and maximize it to get it back, but that should not be necessary.

I can only assume this issue is caused by my attempt to save the position of the application. I do not know what could be going wrong tho, or how I can fix this?

Maybe a way to tell if the app is off-screen after positioning it, and move it on-screen if so? Don't know how I can do that tho?

Any ideas?

Thanks!!!


回答1:


I believe you'll want to change your XML saving code from:

try
{
    s.open(f,flash.filesystem.FileMode.WRITE);

    s.writeUTFBytes(xml.toXMLString());

    s.close();
}
catch(e:Error){}

To

try
{
    s.open(f,flash.filesystem.FileMode.WRITE);
    s.writeUTFBytes(xml.toXMLString());
} catch(e:Error) {
    logger.error( e );
} finally {
    s.close();
}

If an error happens you'll never see it since the catch block just ignores it. If an error is happening for some people the stream is never written to. Also the close call is never executed either so the file is left hanging open. Putting the close() call in the finally block will ensure regardless of success or failure the file will close. It's not a guarantee that this is your problem, but these are, in theory, potential pitfalls some users might be experiencing.

Also you might want to close the file after reading it too.

var s:FileStream = new FileStream();
try {
   s.open(f,flash.filesystem.FileMode.READ);
   var xml:XML = XML(s.readUTFBytes(s.bytesAvailable));
   ...
} finally {
   s.close();
}

It's possible that it fails to save the window location on exit because it can't open the file for writing because it's still open for reading too.

I'd also recommend start writing these errors to a log file to see if an error could be the culprit. AIR has a pretty nice logging system that you can use very similar to log4j. When people are having trouble you can just get them to send you that log file and get better feedback about what's going on in their program for their situation. Logging in any serious application is a must.

Not to mention you can use applications like XPanelOnAIR to get logging messages as they happen during debugging. Much much much better than trace().

http://www.novio.be/blog/?p=920

http://livedocs.adobe.com/flex/3/html/help.html?content=logging_09.html

Removed references to flush() since AIR doesn't have those calls.



来源:https://stackoverflow.com/questions/2335246/air-save-application-location-and-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!