2008-11-27 48 views
2

我有一个降级表面到一个画布容器。 我想链接宽度和高度。 当我使用像它结合按预期工作:Flex绑定和AS3

// binding 
    BindingUtils.bindProperty(rect,"height",this,"height"); 
    BindingUtils.bindProperty(rect,"width",this,"width"); 

现在,有人告诉我,我应该用我目前的柔性知识我不真的知道做它调用validateSize()或validateDisplayList(), 为什么,但我尝试以下内容

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { 

    trace(unscaledWidth, unscaledHeight); 
    this.width = unscaledWidth; 
    rect.width =unscaledWidth; 
    this.height = unscaledHeight; 
    rect.height = unscaledHeight; 

} 

降级矩形调整得很好,但不是'this'画布容器。 他们似乎没有绑定,我错过了什么?

此外,我想修改一些关系rect.width = this.width与一些因素,我不能使用bindproperty方法。

非常感谢任何线索。

+0

该容器大小为零。 – coulix 2008-11-27 17:43:47

回答

0

以防万一,

更多的代码

public class RectangleShape extends BaseShape 
{ 
public function RectangleShape(fillColor:int) { 
    super.name = shapeName; 
    solidFill.color = fillColor; 


    // shape 
    solidFill.alpha = 0.3; 
    rect.fill = solidFill; 
    rect.stroke = solidStroke;  
    geoGroup.geometryCollection.addItem(rect);  
    geoGroup.target = surface; 

    surface.graphicsCollection.addItem(geoGroup); 
    this.addChild(surface); 

    // binding 
    // BindingUtils.bindProperty(rect,"height",this,"height"); 
    // BindingUtils.bindProperty(rect,"width",this,"width"); 
} 




override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { 

    trace(unscaledWidth, unscaledHeight); 
    this.width = unscaledWidth; 
    rect.width =unscaledWidth; 
    this.height = unscaledHeight; 
    rect.height = unscaledHeight; 

} 
1

某处在您的updateDisplayList你应该叫:

super.updateDisplayList(unscaledWidth, unscaledHeight); 
0

updateDisplayList()方法是你应该的大小和位置您的组件的孩子对象。您不应该设置组件本身的大小(即:不要这样做:this.width=100)。

您还可以使用Flash图形API在updateDisplayList()中进行程序化绘图。

updateDisplayList()由Flex调用,并且这两个参数unscaledWidthunscaledHeight是您的组件将呈现的大小。您应该遵守这些维度,并根据它们设置组件子对象的大小。

updateDisplayList()中设置尺寸时,不要直接修改宽度/高度属性。这样做会触发更多的Flex生命周期方法来执行。而应使用子组件的setActualSize()方法。

与设置子对象的x/y属性一样。不要直接设置它们,请使用move()方法。

最后,在Flex 4中,他们为Spark组件添加了一些类似的生命周期方法。尽可能使用它们:setLayoutBoundsSize(),setLayoutBoundsPosition()

这是你原来的updateDisplayList(),修改为每以上:使用第二种情况,当

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { 

    trace(unscaledWidth, unscaledHeight); 

    // like Janroel said above, you should call the super class method 
    // the only time you don't need to is when your component extends UIComponent 
    // (because UIComponent's updateDisplayList doesn't do anything) 
    super.updateDisplayList(unscaledWidth, unscaledHeight); 

    // don't do this (your component will get sized by its parent) 
    //this.width = unscaledWidth; 
    // if 'rect' inherits from UIComponent you should use the setActualSize() method: 
    rect.setActualSize(unscaledWidth, unscaledHeight); 
    // if it doesn't inherit from UIComponent, do it the regular way... 
    rect.width =unscaledWidth; 
    //this.height = unscaledHeight; 
    rect.height = unscaledHeight; 
}