有没有办法在flex中创建带有子视图系统的父视图?例如,我需要创建一个计算不同产品保险费率的应用程序。所有产品都需要具有相同的性别、年龄和尼古丁用量输入。我想做的是有一个“父视图”(实际上不会显示),它布局了所有这些基本字段,然后创建自动显示父视图的组件和布局的子视图,这将减少重复代码。子视图将具有产品特有的附加组件(有些还需要考虑孩子的数量等)并以不同的方式计算费率。
编辑:假设我有 2 种不同的产品。ProdA 和 ProdB
这是 ProdA 的观点
<?xml version="1.0" encoding="utf-8"?>
<components:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" xmlns:components="spark.components.*" title="ProdA"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import ASClasses.LL;
public function makeLL(age:String, gen:String, nic:String):void{
var intAge:int=int(age);
var newLL:LL=new LL(intAge, gen, nic);
dest.text=String(newLL.computeRate());
}
]]>
</fx:Script>
<s:VGroup width="100%" height="100%" verticalAlign="middle" horizontalAlign="center">
<s:Label text="Age:"/>
<s:TextInput id="age" restrict="0-9" maxChars="2"/>
<s:ComboBox id="GenderBox" width="140" prompt="Gender" >
<s:dataProvider>
<mx:ArrayList>
<fx:String>Male</fx:String>
<fx:String>Female</fx:String>
</mx:ArrayList>
</s:dataProvider>
</s:ComboBox>
<s:Label text="The selected gender is: {GenderBox.selectedItem}"/>
<s:ComboBox id="NicotineBox" width="140" prompt="Nicotine Usage">
<s:dataProvider>
<mx:ArrayList>
<fx:String>Smoker</fx:String>
<fx:String>Non-Smoker</fx:String>
</mx:ArrayList>
</s:dataProvider>
</s:ComboBox>
<s:Label text="The selected Nicotine is: {NicotineBox.selectedItem}"/>
<s:Button label="Get Rate" click="makeLL(age.text, GenderBox.selectedItem, NicotineBox.selectedItem)" />
<s:TextInput id="dest" />
<s:Button label="Back" click="navigator.popView()" styleName="back" />
</s:VGroup>
这是 ProdB 的视图
<?xml version="1.0" encoding="utf-8"?>
<components:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" xmlns:components="spark.components.*" title="ProdB"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import ASClasses.OP;
public function makePerson(age:String, gen:String, nic:String):void{
var intAge:int=int(age);
var newOP:OP=new OP(intAge, gen, nic);
dest.text=String(newOP.computeRate());
}
]]>
</fx:Script>
<s:VGroup width="100%" height="100%" verticalAlign="middle" horizontalAlign="center" >
<s:Label text="Age:"/>
<s:TextInput id="age" restrict="0-9" maxChars="2"/>
<s:ComboBox id="GenderBox" width="140" prompt="Gender">
<s:dataProvider>
<mx:ArrayList>
<fx:String>Male</fx:String>
<fx:String>Female</fx:String>
</mx:ArrayList>
</s:dataProvider>
</s:ComboBox>
<s:Label text="The selected gender is: {GenderBox.selectedItem}"/>
<s:ComboBox id="NicotineBox" width="140" prompt="Nicotine Usage">
<s:dataProvider>
<mx:ArrayList>
<fx:String>Smoker</fx:String>
<fx:String>Non-Smoker</fx:String>
</mx:ArrayList>
</s:dataProvider>
</s:ComboBox>
<s:Label text="The selected Nicotine is: {NicotineBox.selectedItem}"/>
<s:Button label="Get Rate" click="makePerson(age.text, GenderBox.selectedItem, NicotineBox.selectedItem)" />
<s:TextInput id="dest" />
<s:Button label="Back" click="navigator.popView()" styleName="back" />
</s:VGroup>
几乎所有的代码都是相同的,除了一些不同之处。我想要一个包含所有重复代码的视图(产品),然后让 ProdA 和 ProdB 扩展这个产品。这样 Product 视图中的所有内容都会同时显示在 ProdA 和 ProdB 中