Flex绑定和AS3

时间:2008-11-27 17:31:59

标签: flex binding degrafa

我将一个degrafa表面放入画布容器中。 我想链接宽度和高度。 当我使用绑定时,它按预期工作:

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

现在,有人告诉我,我应该在validateSize()或updateDisplayList()上执行此操作, 凭借我目前对flex的了解,我不知道为什么,但我尝试了以下

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

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

}

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

另外我想修改一下rect.width = this.width中的一些关系,其中有一些因素我无法使用bindproperty方法。

非常感谢任何线索。

3 个答案:

答案 0 :(得分:1)

您应该在updateDisplayList中的某处调用:

super.updateDisplayList(unscaledWidth, unscaledHeight); 

答案 1 :(得分: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;

}

答案 2 :(得分:0)

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

您还可以使用Flash图形apis在updateDisplayList()中进行编程绘图。

Flex调用

updateDisplayList(),两个参数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;
}