当前位置: 代码迷 >> Web前端 >> UIComponentGlobals 获取格局管理器,使全局布局立即生效
  详细解决方案

UIComponentGlobals 获取格局管理器,使全局布局立即生效

热度:85   发布时间:2012-07-15 20:11:36.0
UIComponentGlobals 获取布局管理器,使全局布局立即生效

?

UIComponentGlobals.mx_internal::layoutManager.validateNow();

?

原理如下:

?

在flex中,主要使用LayoutManager来驱动组件的度量和布局策略, LayoutManager实现一个单例,在Application类构造函数中创建:?

?

public function Application()
     {
         UIComponentGlobals.layoutManager = ILayoutManager(
             Singleton.getInstance("mx.managers::ILayoutManager"));
         UIComponentGlobals.layoutManager.usePhasedInstantiation = true;
 
         if (!FlexGlobals.topLevelApplication)
             FlexGlobals.topLevelApplication = this;
 
         super();
                     
         showInAutomationHierarchy = true;
     }
?

布局分三个阶段执行:提交、度量和布局。

那作为一个组件,是怎么样 参与到布局策略中呢?
flex中所有的组件都继承自UIComponent类,在该类上实现了一个叫IInvalidating的接口:

?

public interface IInvalidating
{

    function invalidateProperties():void;
    function invalidateSize():void;
    function invalidateDisplayList():void;

        function validateNow():void;
}
?

当组件调用invalidateProperties方法,表明该组件一个属性发生变化,需要更新.
它会在方法体写上这么一句:
?? UIComponentGlobals.layoutManager.invalidateProperties(this);
来告诉布局管理器(LayoutManager),布局管理器在适当的时候回调组件上的validateProperties()验证属性,
如果有属性有变化,进而再调用commitProperties()提交变化的属性.

同理,
调用invalidateSize()告诉布局管理器组件大小发生了变化.
调用invalidateDisplayList()告诉布局管理器组件布局发生了变化.