`
keren
  • 浏览: 1563581 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

重写(覆盖)UICompent的核心方法

    博客分类:
  • Flex
阅读更多
覆盖核心 UIComponent 方法
Whenever I am working on a Flex project, it is quite seldom that I have been able to take an out of the box component and have it do EXACTLY what I need to do (barring something like a Label). Sometimes I need to create a container component to use as an itemrenderer for a grid. However, sometimes it is not as easy as whipping up some mxml tags to layout what we need. Luckily though, there are 5 methods that every UIComponent implements* that we are able to override and manipulate our objects as we wish. These methods are createChildren, commitProperties, measure, layoutChrome and updateDisplayList. I will be giving a brief explaining of what they are and why we would override them. After that, I will be discussing the flags within UIComponent.as that call these functions and some things to watch out for when overriding these functions.


createChildren

override protected function createChildren():void{
    super.createChildren();
    if (!_button){
        _button = new Button();
        _button.addEventListener(MouseEvent.CLICK,handleEvent);
        _button.setStyle("color",0xFF0000);
        addChild(_button);
    }
}

By far the easiest to explain, createChildren does just that. This method is pretty much used to add the children of the component. Aside from that, you are able to set some default settings, event listeners and styles.
commitProperties
override protected function commitProperties():void {
 super.commitProperties();
    if (dataChanged){
        isDirty=true;
    }
}

When I have used this, I have used it to set state specific properties of my component. IE: changing the color depending on a value, setting flags, etc. Also if your component implements IListItemRenderer or IDropInListItemRenderer, your data or listData values will be set by the time they your component reaches commitProperties.
measure
override protected function measure():void {
    super.measure();
    this.measuredHeight=16;
    this.percentWidth=100;
    this.minHeight=5;
}

The Measure function is about as easy to explain as creareChildren. Within here, you are able to set your components default height/width, as welll as minimum/maximum height/width. However, the way this function gets called is a little unique when compared to the other functions, while I will discuss in a bit.
layoutChrome
override protected function layoutChrome(unscaledWidth:Number,unscaledHeight:Number):void{
    if(border){
        border.setActualSize(w,h);
    }
}

layoutChrome is used to set the boarders of any Container class that you are extending off of. If any of these functions was the forgotten stepchild, it is this function. I have only run into a situation once where I needed to implement it. However, the one time I got to use it, I pulled my hair out because I ran into an instance where Adobe wanted to protect me and I had to use the mx_internal namespace.
updateDisplayList
override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void{
    super.updateDisplayList(unscaledWidth,unscaledHeight);
    var o:Object;
    var yOffset=10;
    for (var x:int=0;x<numChildren;x++){
        o = getChildAt(x);
        yOffset += o.height;
        o.move(o.x+OFFSET,yOffset);
    }
}

If layoutChrome is the forgotten step child, updateDisplayList is the most popular child, and for good reason. This is the component that sets its position and size (if need be) and the component will not be displayed until it is called. For container objects that have more than one component, this is the function that allows you to manually position those components based off of its current state. A perfect example of this would be an HBox of VBox. Your components are positioned horizontally or vertically within updateDisplayList.
With these five functions, you will be able to create any componet that you can think of. But what calls these functions? Turns out that within UIComponent, there are three flags which are set by these functions:
invalidateProperties()

When this is called, it marks your component to call commitProperties upon the next render update.
invalidateDisplayList()

When this is called, it will call both layoutChrome, as well as updateDisplayList.
invalidateSize()

This is the interesting one. Calling this method will mark the measure() function to be called, but there is one slight variation on this one. If I update the x value on a component, it will be marked as invalid and updateDisplayList will be called upon the next render event. If a data event gets fired, its marked as invalid and commitProperites is called. However, if I change the height, measure will not be called. Flex will only call measure() if you set explicitHeight or explicitWidth to NaN.
What to watch out for
When people implement these functions, sometimes they do not realize the power they have taken responsibility of. They are to be treated with the respective they deserve. First, do not forget to call the super method of each functions. That may drop you in the land of some angry times if you dont. Another thing I have seen is people sometimes get tempted to add children inside of something like commitProperties or updateDisplayList. This may cause serious performance issues. Whenever addChild is called, invalidateProperties and invalidateDisplayList is called, creating a big mess. It is best to use these functions for only their intended purpose.
It is hard to be a Flex programmer with out having to dive into to the world of creating your own components. Out of the box functionality often does not give us what we need. With these functions, you will be able to create any component you need from the ground up.
*Layoutchrome is included in anything that extends the Container class. A large number of components utilize this, but UIComponent does not.

本文来源于 冰山上的播客 http://xinsync.xju.edu.cn
原文地址:http://xinsync.xju.edu.cn/index.php/archives/4869
分享到:
评论

相关推荐

    重写window.alert方法

    重写window.alert方法 重写window.alert方法

    方法重写(覆盖)

    方法重写又称方法覆盖。 若子类中的方法与父类中的某一方法具有相同的方法名、返回类型和参数表,则新方法将覆盖原有的方法。 如需父类中原有的方法,可使用super关键字,该关键字引用了当前类的父类。

    重载与覆写/重写的区别

     方法重写又称方法覆盖。 (2)若子类中的方法与父类中的某一方法具有相同的方法名、返回类型和参数表,则新方法将覆盖原有的方法。  如需父类中原有的方法,可使用super关键字,该关键字引用了当前类的父类...

    重载,重写,覆盖,多态

    重载,重写,覆盖,多态的深刻剖析,具体分析了重载,重写,覆盖,多态之间的区别

    关于重写equals,hashcode以及compareTo方法!

    关于重写equals、hashcode以及compareTo方法! equals()方法是Object类中的一个方法,它用于比较两个对象是否相等。然而,它的默认实现是比较对象的引用(地址),而不是比较对象的实际内容。因此,在某些情况下,...

    重写equals和hashcode方法_equals_重写equals和hashcode方法_

    重写equals和hashcode方法,学习和进步

    c++ 重载、覆盖、重写

    这篇文章主要介绍了C++中重载、重写(覆盖)和隐藏的区别,是C++面向对象程序设计非常重要的概念,需要的朋友可以参考下

    重写toString和equals方法

    重写toString和equals方法的意义以及方法

    js怎么覆盖原有方法实现重写

    主要介绍了js怎么覆盖原有方法实现重写,需要的朋友可以参考下

    C#重写重载与多态

    重写:是指重写基类的方法,在基类中的方法必须有修饰符virtual,而在子类的方法中必须指明override。重载:用于在给定了参数列表和一组候选函数成员的情况下,选择一个最佳函数成员来实施调用。多态:c#的多态性...

    Java重写与重载(区别与用途)

     Java 中重写又叫覆盖,主要是指继承(extends)父类或者实现(implements)接口时将父类或者接口中已经存在的方法进行重新定义。重写的方法在父类或者接口中必须存在。  注意:后文的重写方法与覆盖方法同义 ...

    SafePHP重写一组核心PHP函数以抛出异常而不是在遇到错误时返回false

    Safe PHP:重写一组核心PHP函数以抛出异常,而不是在遇到错误时返回false

    10.java方法的重写.zip

    10.java方法的重写.zip10.java方法的重写.zip10.java方法的重写.zip10.java方法的重写.zip10.java方法的重写.zip10.java方法的重写.zip10.java方法的重写.zip10.java方法的重写.zip10.java方法的重写.zip10.java方法...

    C#重写MessageBox对话框

    C#重写MessageBox对话框C#重写MessageBox对话框C#重写MessageBox对话框C#重写MessageBox对话框C#重写MessageBox对话框C#重写MessageBox对话框C#重写MessageBox对话框

    重写equals方法

    本文讲述了什么时候重写equals方法和如何重写equals方法。

    Java中方法重载与重写的区别

    1、重载  方法的重载是同一个类中多态性的一种表现。  方法重载须遵循如下两条规则  (1)方法名相同。  (2)参数列表不同。...  (4)子类方法不能抛出比所覆盖方法更多的异常。  (5)静态方法不存在重

    静态(static)方法重写(override)

    静态(static)方法重写(override)

    重写GZIPInputStream中相应方法MultiMemberGZIPInputStream

    当使用jdk中GZIPInputStream读取.gz文件时,有时还未到文件结尾,则也会返回-1,该工具类解决了此bug

Global site tag (gtag.js) - Google Analytics