我如何将一个 MXML 文件嵌入另一个 MXML 文件? [英] How would I include an MXML file inline another MXML file?

查看:31
本文介绍了我如何将一个 MXML 文件嵌入另一个 MXML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 MXML 文件中包含一个 MXML 文件,就像您可以使用 include 指令.使用 include 指令将外部文件中的代码在编译时放入原始文件中,并将其置于同一作用域中.

I would like to include an MXML file in my MXML file in the same way you can include an external file in AS3 using the include directive. Using the include directive brings the code from the external file into the original file at compile time placing it in the same scope.

例如

Application.mxml:

<Application>

    <source="external.mxml"/>

</Application>

External.mxml:

<Styles/>

<Declarations>
    <Object id="test"/>
</Declarations>

我需要将此代码/mxml/xml 保留在与原始文件相同的范围内的外部文件中.不要问我为什么要这样做.

I need to keep this code/mxml/xml in the external file in scope with the original. Do not ask me why I want to do this.

另一个例子.这是我当前的代码(简化)全部在 1 个 mxml 文件中:

Another example. Here is my current code (simplified) all in 1 mxml file:

...

File1.mxml
<Button click="clickHandler()"/>

<Script>
public function clickHandler():void {

}
</Script>

...

这是我想要的:

...

File1.mxml
<Group>

    <source="File2.mxml"/>

    <Button click="clickHandler()"/>

<Group>


File2.mxml
<Script>
public function clickHandler():void {
    trace(this); // File1.mxml
}
</Script>

...

我想将我的代码拆分成一个单独的文件...

I want to split my code out into a separate file...

~~ 更新 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~ Update ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

虽然不是我所要求的使用代码隐藏"方案实现了将代码从视图中分解出来的部分功劳.因此,我创建了一个 MXML 文件 MyGroup.mxml 或 MyGroup.as,它扩展了包含 clickHandler 代码的 Group.

Though NOT what I was asking for using a "code behind" scheme achieves partial credit to breaking the code out of the view. So I create a MXML file, MyGroup.mxml or MyGroup.as and that extends Group that contains the clickHandler code.

这个方法的问题是我被锁定到我正在扩展的类类型,对视图进行硬编码.因此,例如,如果我想拆分为单独文件的 MXML 类是一个组,我将不得不扩展组.

The problem with this method is that I am then locked to the class type I'm extending, hardcoding the view. So for example I would have to extend Group if the MXML class I want to split into separate files is a Group.

我曾参与过这样的项目,但效果并不好.人们开始在类背后的代码中设置样式和视觉方面或分组/查看特定属性,稍后如果或当我们需要更改它或它的布局时,我们最终会对容器产生所有这些依赖关系.它变得一团糟.另外,使用代码隐藏你不能重用它(重用方式包括样式.as 被重用).所以这不是解决方案,但我想我会提到它.

I've worked on projects where this was done and it is not good. People start setting styles and visual aspects or group / view specific properties in the code behind class and later if or when we need to change it or the layout it we have end up with all these dependencies to the container. It becomes a mess. Plus, using Code Behind you can't reuse it (reuse in the way include styles.as is reused). So this is not a solution but thought I'd mention it.

这是一个代码背后的例子,

Here is a code behind example,

MyGroupBehind.mxml:

<Group>

   <Script>
   public function clickHandler():void {
       trace(this); // File1.mxml
   }
   </Script>

</Group>

MyGroupAhead.mxml:

<MyGroupBehind>

    <Button click="clickHandler()"/>

</MyGroupBehind>

推荐答案

MXML 被编译器转换成类,所以没有办法做你所要求的.

MXML is converted into a class by the compiler, so there is no way to do what you are asking.

就我个人而言,我认为这是一件好事.将事情分解成单独的文件并不等于更有条理".事实上,我会说它达到了完全相反的效果.恕我直言,您最好专注于适当的组件结构.

Personally, I think that is a good thing. Breaking things up into separate files does not equal "more organized". In fact I would say it achieves the exact opposite effect. You would do better to focus on a proper component structure, IMHO.

这篇关于我如何将一个 MXML 文件嵌入另一个 MXML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆