Flex - 创建新的基元
首先,最好说的是我对 Flex / OOP 总体来说是新手。我一直在尝试添加一个基于 StrokedElement 的自定义类,以实现一个简单的网格(不像现有的 Flex 网格 - 这只是用于显示 - 不保存元素等...)
如下所示:
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:lines ,那么我会收到错误。显然我很高兴它有效 - 但我试图理解为什么这里有差异?
To start - its best to say im new to Flex / OOP in general. I have been trying to add a custom class based on StrokedElement in order to implement a simple grid (not like the existing Flex Grids - this would just be for display - not holding elements etc...)
My current class looks like this:
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);
}
}
}
}
Which is cribbed from the Flex spark.primatives.rect - everything works OK - but when I add it to my application I would expect to do this:
<helpers:SGrid id="gridOne" width="100" height="200" gridSize="10">
<s:stroke>
<s:SolidColorStroke color="0xCCCCCC" alpha="0.8" />
</s:stroke>
</helpers:SGrid>
but in actual fact this works instead:
<helpers:SGrid id="gridOne" width="100" height="200" gridSize="10">
<helpers:stroke>
<s:SolidColorStroke color="0xCCCCCC" alpha="0.8" />
</helpers:stroke>
</helpers:SGrid>
If I use the s:stroke then I get errors. Obviously i'm pleased it works - but i'm trying to understand why the difference here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与类的声明名称空间有关。
SGrid
是helpers:
命名空间的一部分,而不是s:
命名空间。因此,在设置其属性时,您需要通过
helpers:
命名空间引用该属性。该属性本身是在 SGrid 的基类上声明的(在您的例子中为
StrokedElement
),这并不重要,它是SGrid
实例的属性。它与以下内容相同:
即使在基类上声明了描边,您也可以通过 SGrid 类引用它。
It's to do with the declared namespace of the class.
SGrid
is part of thehelpers:
namespace, not thes:
namespace.Therefore, when setting it's properties you need to reference the property via the
helpers:
namespace.It doesn't matter that the property itself was declared on a baseclass of SGrid (in your case,
StrokedElement
), it's a property of the instance ofSGrid
.It's the same as:
Even though stroke is declared on the base class, you reference it through the SGrid class.