开发者

AIR: save application location and size

开发者 https://www.devze.com 2022-12-21 10:35 出处:网络
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.

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.toXMLSt开发者_如何学JAVAring());

        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!!!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号