通过将字符串转换为类来推送视图?

时间:2012-04-27 18:57:50

标签: flex mobile flex4 flash-builder

我目前正在使用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)));

有没有人知道如何查看列表中选择的值然后使用该字符串名称将app推送到视图,因为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 个答案:

答案 0 :(得分:2)

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

请添加包名称:com.robaldred.composers.

答案 1 :(得分:0)

getDefinitionByName(selectedItem)几乎就是你想要的,但并不完全。请注意,selectedItem是您的数组中的一项,例如{label:"All Jobs", data:"JList"},而不仅仅是"JList"

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


getDefinitionByName(selectedItem.data)应该对你有用。例如,selectedItem.data应该是JList,然后getDefinitionByName应该返回JList Class对象。

答案 2 :(得分: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)

答案 3 :(得分:0)

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