2

首先,最好说我是 Flex / OOP 的新手。我一直在尝试添加一个基于 StrokedElement 的自定义类,以实现一个简单的网格(不像现有的 Flex Grids - 这只是用于显示 - 不包含元素等......)

我现在的班级是这样的:

package ui.helpers
{
    import flash.display.Graphics;

    import spark.primitives.supportClasses.StrokedElement;

    public class SGrid extends StrokedElement
    {
        public function SGrid()
        {
            super();
        }

        private var _gridSize:Number;
        [Inspectable(category="General", minValue="1.0")]

        public function get gridSize():Number 
        {
            return _gridSize;
        }

        public function set gridSize(value:Number):void
        {        
            if (value != _gridSize)
            {
                _gridSize = value;
                invalidateSize();
                invalidateDisplayList();
                invalidateParentSizeAndDisplayList();
            }
        }

        override protected function draw(g:Graphics):void {

            for(var x:int; x < width; x+= _gridSize) {
                g.moveTo(x,0);
                g.lineTo(x,height);
            }
            for(var y:int; y < height; y+= _gridSize) {
                g.moveTo(0,y);
                g.lineTo(width,y);
            }

        }

    }
} 

摘自 Flex spark.primatives.rect - 一切正常 - 但是当我将它添加到我的应用程序时,我希望这样做:

<helpers:SGrid id="gridOne" width="100" height="200" gridSize="10">
        <s:stroke>
            <s:SolidColorStroke color="0xCCCCCC" alpha="0.8" />
        </s:stroke>
    </helpers:SGrid>

但实际上这是可行的:

<helpers:SGrid id="gridOne" width="100" height="200" gridSize="10">
        <helpers:stroke>
            <s:SolidColorStroke color="0xCCCCCC" alpha="0.8" />
        </helpers:stroke>
    </helpers:SGrid>

如果我使用 s:stroke 则会出现错误。显然我很高兴它有效 - 但我试图理解为什么这里有区别?

4

1 回答 1

2

这与类的声明名称空间有关。

SGridhelpers:命名空间的一部分,而不是s:命名空间。

因此,在设置它的属性时,您需要通过helpers:命名空间引用该属性。

属性本身是在 SGrid 的基类(在您的情况下为StrokedElement)上声明的并不重要,它是SGrid.

它与以下内容相同:

var grid:SGrid = new SGrid();
grid.stroke = new SolidColourStroke(); 

即使在基类上声明了 stroke,您也可以通过 SGrid 类引用它。

于 2011-04-13T13:08:55.390 回答