如何访问中的组件在 MXML 类中?

发布于 2024-12-22 10:17:07 字数 1085 浏览 1 评论 0原文

是否可以在 MXML 类中将声明的组件作为 IFactory 进行访问?我已经多次使用这种声明工厂的方式来声明皮肤部件,但我从未弄清楚如何从 MXML 中访问这些工厂。

下面是我期望的工作示例:

<fx:Declarations>
    <fx:Component id="labelDisplay">
        <s:Label fontSize="12" fontWeight="bold"/>
    </fx:Component>
</fx:Declarations>

<fx:Script>
    <![CDATA[
        override protected function createChildren():void
        {
            super.createChildren();

            var label1:Label = labelDisplay.newInstance();
            addElement(label1);

            var label2:Label = labelDisplay.newInstance();
            addElement(label2);

            var label3:Label = labelDisplay.newInstance();
            addElement(label3);
        }
    ]]>
</fx:Script>

* 编辑 *

我希望上述代码能够工作的原因是基于 Spark 蒙皮架构中处理动态蒙皮部件的方式。如果上面的代码是 MXML 外观类的一部分,那么在我的主机组件中,我可以有以下内容。

[SkinPart(required="true",type="spark.controls.Label")]
public var labelDisplay:IFactory;

在 Spark 换肤架构中,在什么时候起作用?变成一个IFactory?

Is it possible to access a declared component as an IFactory within an MXML class? I've used this style of declaring factories many times for Skin Parts, but I've never figured out how to access those factories from within the MXML.

Here's an example of what I would expect to work:

<fx:Declarations>
    <fx:Component id="labelDisplay">
        <s:Label fontSize="12" fontWeight="bold"/>
    </fx:Component>
</fx:Declarations>

<fx:Script>
    <![CDATA[
        override protected function createChildren():void
        {
            super.createChildren();

            var label1:Label = labelDisplay.newInstance();
            addElement(label1);

            var label2:Label = labelDisplay.newInstance();
            addElement(label2);

            var label3:Label = labelDisplay.newInstance();
            addElement(label3);
        }
    ]]>
</fx:Script>

* edit *

The reason I was hoping the above code would work is based on the way dynamic Skin Parts are handled in the Spark skinning architecture. If the above code were a part of an MXML skin class, then in my host component, I could have the following.

[SkinPart(required="true",type="spark.controls.Label")]
public var labelDisplay:IFactory;

In the Spark skinning architecture, at what point does the <fx:Component> turn into an IFactory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2024-12-29 10:17:07

我深入研究了 Flex 的 SkinnableComponent,根据他们如何将 MXML Skin 绑定到 AS HostComponent 来找到解决方案。显然,即使“labelDisplay”没有作为具体类成员显示在 FlashBuilder 的自动完成中,您仍然可以将其作为动态属性引用。我在这里修改了原来的示例:

<fx:Declarations>
    <fx:Component id="labelDisplay">
        <s:Label fontSize="12" fontWeight="bold"/>
    </fx:Component>
</fx:Declarations>

<fx:Script>
    <![CDATA[
        override protected function createChildren():void
        {
            super.createChildren();

            var labelFactory:ClassFactory = this["labelDisplay"];

            var label1:Label = labelFactory.newInstance();
            addElement(label1);

            var label2:Label = labelFactory.newInstance();
            addElement(label2);

            var label3:Label = labelFactory.newInstance();
            addElement(label3);
        }
    ]]>
</fx:Script>

I dug into Flex's SkinnableComponent to find the solution based on how they tie an MXML Skin to an AS HostComponent. Apparently, even though "labelDisplay" doesn't show up in FlashBuilder's autocomplete as a concrete class member, you can still reference it as a dynamic property. I've modified my original example here:

<fx:Declarations>
    <fx:Component id="labelDisplay">
        <s:Label fontSize="12" fontWeight="bold"/>
    </fx:Component>
</fx:Declarations>

<fx:Script>
    <![CDATA[
        override protected function createChildren():void
        {
            super.createChildren();

            var labelFactory:ClassFactory = this["labelDisplay"];

            var label1:Label = labelFactory.newInstance();
            addElement(label1);

            var label2:Label = labelFactory.newInstance();
            addElement(label2);

            var label3:Label = labelFactory.newInstance();
            addElement(label3);
        }
    ]]>
</fx:Script>
两相知 2024-12-29 10:17:07

我看到的问题是您没有创建 iFactory,因此无法访问 iFactory 方法。

如果您需要的话,我建议您使用 ActionScript 并实际创建一个 iFactory。这使用 ClassFactory

protected var labelDisplay: iFactory = new ClassFactory(spark.controls.Label);

然后你的createChildren 代码应该按原样工作。

The problem I see is that you didn't create an iFactory, so won't be able to access iFactory methods.

I would recommend you drop into ActionScript and actually create an iFactory if you need one. This uses the ClassFactory class

protected var labelDisplay: iFactory = new ClassFactory(spark.controls.Label);

Then your createChildren code should work as is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文