Flex 3:同步加载一个xml文件 [英] Flex 3: synchronously loading an xml file

查看:25
本文介绍了Flex 3:同步加载一个xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单:在flex3中,有没有办法同步加载一个xml文件?

My question is very simple: In flex3, is there a way to load an xml file synchronously?

我知道如何使用加载事件异步加载.这可能有用,也可能没有.我只想读取文件,解析它,做我必须做的事情,然后继续执行代码.

I know how to load asynchronously, using a load event. This may be useful, or may not. I just want to read the file, parse it, do what I have to do with it, and continue executing code.

我有一个组件,它使用一个 xml 文件来存储一些配置参数.我需要在对象初始化时读取文件.但是,使用事件模型,我无法控制文件何时加载,因此我必须编写代码以等待"代码加载.这太荒谬了,还是我?我想要这样的代码:

I have a component that uses an xml file to store some configuration parameters. I need to read the file when the object is initialized. However, with the event model, I can't control when the file is loaded, so I must write code to "wait" for the code to load. This is just ridiculous, or is it me? I want code like this:

var foo:Foo = new Foo(); //This constructor should read the xml and initialize the object.
foo.doSomething(); //When I call this method the xml must be already handled.

我可以处理事件上的 xml 文件,并且工作正常,但是在 doSomething 方法之后触发事件.

I can handle the xml file on the event, and it works fine, but the event fires after the doSomething method.

我希望我已经解释了自己.我认为这应该很容易,但它让我发疯.除非真的有必要,否则我不想编写代码来等待事件.我觉得这一切应该只是一行代码!

I hope I have explained myself. I think this should be really easy, but it's driving me crazy. I don't want to write code to wait for the event unless it's really necessary. I feel all this should be just one line of code!

推荐答案

无法同步加载,Flash 是为 Web 构建的,您永远无法确定调用需要多长时间.Air 是不同的,因为它从文件系统加载,并且那里的延迟量远不相同.

It's not possible to load synchronously, flash is built for the web and you can never be sure how long a call takes. Air is different because that loads from the filesystem, and there are nowhere near the same amounts of delay there.

最干净的解决方案是在 Foo 内部监听负载完成并从内部调用 doSomething(),这样你的外部"类就不需要打扰了.

The cleanest solution would be to listen for the load to complete inside Foo and calling doSomething() from inside, this way your "outer" class won't need to bother at all.

如果您确实需要从外部调用 foo.doSomething(),则可以使用事件系统.让你的 Foo 类在加载完成后调度一个事件:

If you do absolutely need to call foo.doSomething() from the outside, you can use the event system. Let your Foo class dispatch an event when it is done loading:

dispatchEvent(new Event(Event.COMPLETE, true));

要抓住这一点,您需要像这样听:

To catch that you will need to listen for it like so:

foo.addEventListener(Event.COMPLETE, handleFooComplete);

你的事件处理函数应该是这样的:

And you event handler function should look like this:

private function handleFooComplete(e:Event):void{
    foo.doSomething();
}

无论你选择做什么,你都需要在加载器上监听 Event.COMPLETE.没有办法解决这个问题.

However you choose to do it, you will need to listen for Event.COMPLETE on the loader. There's no getting around that.

这篇关于Flex 3:同步加载一个xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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