AIR:保存应用程序位置和大小 [英] AIR: save application location and size

查看:23
本文介绍了AIR:保存应用程序位置和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在努力做到这一点,当用户启动我的 air 应用程序时,它会根据上次打开时的位置和大小来调整自己的位置.

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.

这是我的代码:

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?

有什么想法吗?

谢谢!!!

推荐答案

我相信您会想要更改您的 XML 保存代码:

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){}

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

如果发生错误,您将永远不会看到它,因为 catch 块会忽略它.如果某些人发生错误,则永远不会写入流.此外,关闭调用也永远不会执行,因此文件保持打开状态.将 close() 调用放在 finally 块中将确保无论成功或失败文件都将关闭.不能保证这是您的问题,但从理论上讲,这些是某些用户可能遇到的潜在陷阱.

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.

我还建议开始将这些错误写入日志文件,以查看错误是否可能是罪魁祸首.AIR 有一个非常好的日志系统,您可以使用它与 log4j 非常相似.当人们遇到问题时,您可以让他们向您发送该日志文件,并获得有关他们的程序中针对他们情况发生的情况的更好反馈.必须登录任何严肃的应用程序.

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.

更不用说您可以使用 XPanelOnAIR 等应用程序来获取调试期间发生的日志消息.比 trace() 好多了.

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

删除了对 flush() 的引用,因为 AIR 没有这些调用.

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

这篇关于AIR:保存应用程序位置和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆