Use CallLater()方法

来源:互联网 发布:用php开发简单的商城 编辑:程序博客网 时间:2024/05/19 01:33

The callLater() method queues an operation to be performed for the next screen refresh(下一帧), rather than in the current update. Without the callLater() method, you might try to access a property of a component that is not yet available.

Have you ever tried to do something like this:

var someText:Text = new Text();
someText.text="testing";
addChild(someText);
trace(someText.measuredWidth);
and then scratched your head when Flex traces 0 for the width? That's because Flex hasn't drawn it yet. There's two ways to fix it, one you could just call someText.validateSize() but the better way to do it is to wait till Flex validates on its own to make these changes. How do you do that? Well with the callLater() function of course!
var someText:Text = new Text();
someText.text="testing";
addChild(someText);
callLater(getWidth);
public function getWidth():void{
     trace(someText.measuredWidth);
}
 
ahhh... there we go. Now Flex should be tracing the correct width. This works really well for a number of instances where you want to play with freshly declared visual elements but Flex hasn't finished creating them yet.

callLater vs. validateNow

For years, many Flex developers have known the secret to fixing many a UI problem was using either callLater() or validateNow() methods. Much to my surprise, some of these developers do not understand WHY these fix problems, what the difference is, and which method to use.

So why do we need these functions? Well, in short, the flex framework doesn't do anything in the order you tell it and it doesn't do what you tell it to do when you tell it to. If you set a text's width and a text's style, they may not be applied in that order. And they will probably not be applied until a bunch of other stuff has happened either. When a programmer thinks something is done synchronously (one after the other) when they're actually being done according to the component when it "validates". It is important to note that this is not an issue with ActionScript 3, so much as an issue with the Flex Framework. 
callLater vs. validateNow

callLater accepts two parameters. A function and some arguments for the function. This "function" gets queued to be called again at a later frame, hopefully after everything else has finished processing..

validateNow accepts no parameters and basically tells your component to just do its processing RIGHT NOW! So the difference is, instead of waiting and executing, like callLater, it just executes right now. From a programmer's perspective, this is a lot easier to do, however, it is not faster. It forces Flex to reprocess a bunch of stuff, whereas callLater just executes the code you wanted to execute anyway at a later time. If you want to use validateNow, consider that there are some more specific functions such as validateSize, validateProperties, and validateDisplayList. All 3 of these functions are called by validateNow.

Code vs. Code

The situation is that you want the width of some rendered text. After setting the text, you can not get the measuredwidth property because it will still be 0 until the component has validated.

CallLater Solution
var someText:Text = new Text();
someText.text="testing";
addChild(someText);
trace("before: " + someText.measuredWidth);
callLater(getWidth);
public function getWidth():void{
     trace("after: " + someText.measuredWidth);
}
validateNow Solution
var someText:Text = new Text();
someText.text="testing";
addChild(someText);
trace("before: " + someText.measuredWidth);
someText.validateNow(); //you should actually use validateSize() if you just want the measured width
trace("after: " + someText.measuredWidth);

Hopefully this makes sense. In my opinion, it is generally better to use callLater as it won't cause any unneeded computation.


转载:http://leetechlife.blogbus.com/logs/82845226.html

原创粉丝点击