1

我目前正在使用 Flashbuilder 4.6 创建移动应用程序,我希望根据用户选择的菜单项将用户推送到视图。下面的代码显示了我如何尝试将选定列表项中的字符串转换为类对象,然后将其传递给 pushView 的参数:

<fx:Script>
    <![CDATA[
        import mx.collections.*;            
        import spark.components.SplitViewNavigator;
        import spark.components.ViewNavigator;
        import spark.events.IndexChangeEvent;
        import spark.events.ViewNavigatorEvent;
        import spark.managers.PersistenceManager;
        import spark.skins.mobile.SplitViewNavigatorSkin;

        public var menuAC:ArrayCollection;          
        public var j_array:Array = [{label:"Home", data:"NLView"}, 
            {label:"All Jobs", data:"JList"}, 
            {label:"Company Profiles", data:"SView"}, 
            {label:"Search By Sector", data:"JView"}];


        protected function list_changeHandler(event:IndexChangeEvent):void
        {
            var selectedItem:String = new String(list.selectedItem.data);
            //trace("Selected Item = " + selectedItem);
            var splitNavigator:SplitViewNavigator = navigator.parentNavigator as SplitViewNavigator;
            var detailNavigator:ViewNavigator = splitNavigator.getViewNavigatorAt(0) as ViewNavigator;
            splitNavigator.hideViewNavigatorPopUp();

            if(list.selectedItem.data == "NewListView")
            {
                var myClass:Class = getDefinitionByName(selectedItem) as Class;                 
                trace("View Class = " + myClass);
                detailNavigator.pushView(myClass);
            }
            else if(list.selectedItem.data)
            {

            }


        }

        protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
        {

            if(data.view == "JSFilt")
            {
                menuAC = new ArrayCollection(j_array);  
            }

            list.dataProvider = menuAC;
            // TODO Auto-generated method stub
            var splitNavigator:SplitViewNavigator = navigator.parentNavigator as SplitViewNavigator;
            var detailNavigator:ViewNavigator = splitNavigator.getViewNavigatorAt(1) as ViewNavigator;
            splitNavigator.hideViewNavigatorPopUp();
        }


    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<s:BitmapImage width="100%" height="100%" source="@Embed('assets/images/sidebg.png')" fillMode="repeat"/>
<s:BitmapImage horizontalCenter="0" top="5" width="175" height="130" source="@Embed('assets/images/G_S_Logo_170x130.png')"/>

<s:List id="list" left="0" right="0"
        color="0xFFFFFF" contentBackgroundColor="0x101010"
        fontSize="20" width="280"
        alternatingItemColors="[0x202020,0x2a2a2a]"
        top="140" horizontalCenter="0" bottom="200"
        downColor="0x70B2EE"
        selectionColor="0x70B2EE"
        change="list_changeHandler(event)"> 
    <s:itemRenderer>
        <fx:Component>
            <s:IconItemRenderer messageStyleName="myFontStyle" fontSize="22" height="70" 
                                labelField="label"
                                messageField="" 
                                />
        </fx:Component>
    </s:itemRenderer>       
</s:List>

我也尝试过使用以下命令,但也没有用:

var viewClass:Class = Class(getDefinitionByName(getQualifiedClassName(selectedItem)));

有没有人知道如何查看列表中选择的值,然后使用该字符串名称将应用程序推送到视图,因为 pushView 方法不接受字符串?


感谢您的建议,我喜欢将类添加到数组然后将其传递给 pushView 的想法。我过去曾尝试过,但它没有用,所以我用下面的代码尝试了你的方法:

public var jobs_array:Array = [{label:"Home", data:"NewListView", class:NewListView},
                {label:"All", data:"JList"}, 
                {label:"C Prof's", data:"SView"}, 
                {label:"S By S", data:"JSView"}];

如您所见,我已尝试实施您在第一行建议的内容,但出现以下错误:

1084: Syntax error: expecting colon before rightbrace.
1084: Syntax error: expecting identifier before class.
1084: Syntax error: expecting identifier before rightbrace.

这必须是一个简单的语法,但我看不到,有人可以帮我吗?

4

4 回答 4

2
var imageClass:Class=getDefinitionByName("com.robaldred.composers."+imgclass) as Class;

请像这样添加包名称:com.robaldred.composers.

于 2012-12-14T06:46:15.287 回答
0

是的..这是使用字符串 var str:String="roof"; 绑定 spark viewstack slelected child 的答案。viewstack.selectedChild=NavigatorContent(viewstack.getChildByName(str));

于 2012-12-14T10:31:44.533 回答
0

为什么不直接在数据中放一个对 Class 本身的引用,不用担心使用 String 来表示它。

您的数据项将如下所示:

var data:Array = [
{ label: "Item 1", class: ClassToUseForItem1 },
{ label: "Item 2", class: ClassToUseForItem2 },
{ label: "Item 3", class: ClassToUseForItem3 } ];

现在你可以做pushView(list.selectedItem.class)

于 2012-04-28T17:31:53.497 回答
0

getDefinitionByName(selectedItem)几乎是你想要的,但不完全是。看,selectedItem是您数组中的项目之一,例如{label:"All Jobs", data:"JList"},不仅仅是"JList".

getDefinitionByName(getQualifiedClassName(selectedItem))绝对不是您想要的,因为getQualifiedClassName将返回selectedItem. 在这种情况下,那将是Object. 然后getDefinitionByName将返回 Object 类。


getDefinitionByName(selectedItem.data)应该适合你。例如,selectedItem.data应该是,然后应该返回 JList 类对象。JListgetDefinitionByName

于 2012-04-27T19:15:37.510 回答