我想控制主应用程序上ViewStack的selectedIndex属性。我在main.mxml中分配了一个变量。我试图通过自定义组件中的函数来操作该变量; viewControl.mxml。我已经能够用buttonBar模拟效果,但我宁愿用按钮来完成。使用按钮组件切换selectedIndex
Main.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:comps="comps.*" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import comps.viewControl;
[Bindable]
public var mainIndex:int = 0;
]]>
</fx:Script>
<comps:viewControl id="myControl"/>
<mx:ViewStack id="lgViewStack" selectedIndex="{mainIndex}">
<s:NavigatorContent id="view1">
<s:Panel id="firstPanel">
</s:Panel>
</s:NavigatorContent>
<s:NavigatorContent id="view2">
<s:Panel id="secondPanel">
</s:Panel>
</s:NavigatorContent>
</mx:ViewStack>
和组件viewControl.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<fx:Script>
<![CDATA[
protected function changeView(index:int):void
{
mainIndex = index;
}
]]>
</fx:Script>
<s:Button id="myButton" click="changeView(1);"/>
在编译时,我收到以下错误信息:未定义的属性mainIndex的访问在viewControl.mxml中。有什么我可以做的功能集或功能获取从Main.mxml mainIndex?
嗯。你在变量的范围上和你之前的问题有完全相同的问题http://stackoverflow.com/q/5719389/680503 – Constantiner 2011-04-19 21:44:00
Flex中的变量范围是一个给我带来问题的概念。我还没有找到专门为我的目的解释它的资源。如果我从主应用程序创建一个公共函数或变量,那么直接附加的组件应该能够读/写它们。 – smulholland2 2011-04-19 22:17:42
你是否清楚地了解面向对象的概念? Flex MXML文件只是普通的类。所以没有区别。 – Constantiner 2011-04-20 07:24:45