这种执行没有约束力——如果因疏漏而发生改变,则不会反映在你的显示名单上。
虽然我认为,这在创建时应当一劳永逸:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955"
minHeight="600">
<fx:Script>
<![CDATA[
private function foo():Boolean
{
return false;
}
]]>
</fx:Script>
<s:Button label="text"
visible="{foo()}" />
</s:Application>
更好的做法是纳入一个介绍模式,利用这里所看到的具有约束力的手段:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955"
minHeight="600"
creationComplete="creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
[Bindable]
public var presentationVisible:Boolean = true;
private var timer:Timer = new Timer(500);
private function foo():void
{
presentationVisible = Math.random() > 0.5 ? true : false;
}
protected function creationCompleteHandler(event:FlexEvent):void
{
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
protected function timerHandler(event:TimerEvent):void
{
foo();
}
]]>
</fx:Script>
<s:Button label="text"
visible="{presentationVisible}" />
</s:Application>