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

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

问题描述

OK,我想使它这样,当用户启动我的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.

下面是我的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?

任何想法?

谢谢!

推荐答案

我相信你会想从改变你的XML节省code:

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有pretty的很好的记录系统,你可以用非常类似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应用程序来获得登录消息,因为它们在调试过程中发生的。比跟踪得多好多了()。

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/柔性/ 3 / HTML / help.html?内容= logging_09.html

删除引用的flush()由于空气不具有这些呼叫。

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

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

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