如何将MXML文件包含在另一个MXML文件中? [英] How would I include an MXML file inline another MXML file?

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

问题描述

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



例如,

Application.mxml:

 < Application> 

< source =external.mxml/>

< / Application>

External.mxml:

 < Styles /> 

<声明>
< Object id =test/>
< /声明>

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

另一个例子。这是我目前的代码(简体)所有在1 mxml文件:

...

...

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

< Script>
public function clickHandler():void {

}
< / Script>

...



我想:

$ p
$ b $ $ $ $ $ $ $ $ File1.mxml
< Group>

< source =File2.mxml/>

< Button click =clickHandler()/>

< Group>


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

...



我想将我的代码分割成单独的文件...



~~ 更新 ~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ $ / $ $ $
$ b

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

这个方法的问题是,我被锁定到我正在扩展的类类型,硬编码视图。所以例如,我将不得不扩展组,如果我想要分割成单独的文件的MXML类是一个组。



我已经完成了这个项目,并且不好。人们开始设置样式和视觉方面,或者在类后面的代码中对特定的属性进行分组/查看特定的属性,以后如果或者当我们需要改变它时,或者我们最终得到的所有这些对于容器的依赖关系。它变得一团糟。另外,使用代码隐藏你不能重用它(重用的方式包括styles.as被重用)。所以这不是一个解决方案,但认为我会提到它。



以下是示例背后的代码: MyGroupBehind.mxml

 < Group> 

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

< / Group>

MyGroupAhead.mxml:

 < MyGroupBehind> 

< Button click =clickHandler()/>

< / MyGroupBehind>


解决方案

MXML由编译器转换为类,所以没有办法做你在问什么。



我个人认为这是一件好事。把事情分解成单独的文件并不等于更有组织。事实上,我会说它达到了完全相反的效果。你最好把重点放在适当的组件结构,恕我直言。


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.

For example,

Application.mxml:

<Application>

    <source="external.mxml"/>

</Application>

External.mxml:

<Styles/>

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

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.

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

...

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

<Script>
public function clickHandler():void {

}
</Script>

...

Here is what I want:

...

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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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.

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.

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 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天全站免登陆