我可以在 MXML 的一个文件中包含多个类吗?

发布于 2024-11-17 08:09:05 字数 166 浏览 4 评论 0原文

我有一个仅在 A 类内部使用的 B 类。然而,classA 是用mxml 编写的,而不是actionscript 代码。是否可以在 MXML 中嵌套类或在同一 .mxml 文件中的根标记后添加另一个类? 澄清:我希望在同一个文件中用 MXML 编写这两个类,但我在 Adob​​e 文档中找不到指定具体方式的任何内容。

I have a classB that will only be used inside classA. However, classA is written as mxml, not actionscript code. Is it possible to nest classes in MXML or add another class after the root tag in the same .mxml file?
Clarification: I want both classes written in MXML within the same file, but I couldn't find anything in the Adobe documentation that specified how.

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

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

发布评论

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

评论(3

哥,最终变帅啦 2024-11-24 08:09:05

不,您不能在一个 MXML 文件中定义两个类,但您可以为这两个类使用相同的包(命名空间)并将 classB 设置为 internal,因此它仅对该包内的类可见。

No, you can't define two classes in one MXML file, but you can have the same package (namespace) for both classes and make classB internal, so its only visible for classes within that package.

南渊 2024-11-24 08:09:05

我相信您正在寻找 fx:Component 标签允许您在现有 MXML 文档中定义新的 MXML 文档:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:local="*">

    <fx:Declarations>
        <fx:Component className="MyMXMLClass1">
            <s:Group>
                <s:Button label="MyMXMLClass1" />
            </s:Group>
        </fx:Component>
        <fx:Component className="MyMXMLClass2">
            <s:Group>
                <s:Button label="MyMXMLClass2" />
            </s:Group>
        </fx:Component>
    </fx:Declarations>

    <s:VGroup>
        <local:MyMXMLClass1 />
        <local:MyMXMLClass2 />
    </s:VGroup>

</s:Application>

I believe you are looking for the fx:Component tag that allows you to define a new MXML document within an existing MXML document:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:local="*">

    <fx:Declarations>
        <fx:Component className="MyMXMLClass1">
            <s:Group>
                <s:Button label="MyMXMLClass1" />
            </s:Group>
        </fx:Component>
        <fx:Component className="MyMXMLClass2">
            <s:Group>
                <s:Button label="MyMXMLClass2" />
            </s:Group>
        </fx:Component>
    </fx:Declarations>

    <s:VGroup>
        <local:MyMXMLClass1 />
        <local:MyMXMLClass2 />
    </s:VGroup>

</s:Application>
心是晴朗的。 2024-11-24 08:09:05

如果嵌套类中需要多级继承,则 (在前面的答案中提到)的替代方法是使用 > 例如:

<fx:Library>
    <fx:Definition
        name="MyClass"
        >
        <s:Group>
        ...
        <s:/Group>
    </fx:Definition>
</fx:Library>

...
<!-- Use MyClass later in the file. -->
<fx:MyClass ... />

必须位于 MXML 文件的顶部。此语法允许在一行中存在多个嵌套类定义,并且每个类定义都可以通过继承来扩展前一个类定义。

If multiple levels of inheritance are required in the nested classes, and alternative to <fx:Component> (mentioned in a previous answer) is to use <fx:Library> for example:

<fx:Library>
    <fx:Definition
        name="MyClass"
        >
        <s:Group>
        ...
        <s:/Group>
    </fx:Definition>
</fx:Library>

...
<!-- Use MyClass later in the file. -->
<fx:MyClass ... />

The <fx:Library> must be at the top of the MXML file. This syntax allows for several nested class definitions in a row, and each can extend the previous via inheritance.

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