在 Flex 4 中动态加载组件

发布于 2024-12-11 14:54:48 字数 2037 浏览 0 评论 0原文

我有一个应用程序,可以在 TabNavigator 中动态创建选项卡。正如你所看到的,根据我的代码,我基本上有 5 个带有特定名称的选项卡。现在我还有 5 个与选项卡同名的 mxml 组件(即 mxml 组件的名称与选项卡相同,即 Tab1、Tab2 等。非动态方法是使用 myTab1:Tab1 = new Tab1();myTab1:Tab2 = 新 Tab2);因此,我必须对每个我不想要的选项卡执行此操作。 我想要做的是在循环遍历数组时在每个选项卡中加载 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" minWidth="955" minHeight="600">
        <fx:Script>
            <![CDATA[
                import mx.containers.VBox;
                import mx.controls.Label;
                import mx.events.FlexEvent;
                import spark.components.NavigatorContent;


                protected function tabNavigator_creationCompleteHandler(event:FlexEvent):void
                {

                    var superModules:Array =["Tab1","Tab2","Tab3","Tab4","Tab5"];
                    for each (var superModule in superModules)
                    {
                        var myNavigatorContent:NavigatorContent = new NavigatorContent();
                        myNavigatorContent.percentHeight = 100;
                        myNavigatorContent.percentWidth = 100;
                        myNavigatorContent.label = superModule;
                        myNavigatorContent.addChild(superModule as DisplayObject);
                                        tabNavigator.addChild(myNavigatorContent);

                    }   


                }

            ]]>
        </fx:Script>
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
        <mx:TabNavigator id="tabNavigator" width="100%" height="100%" creationComplete="tabNavigator_creationCompleteHandler(event)">
        </mx:TabNavigator>
    </s:Application>

  Can somebody help on achieving this. 
    Many thanks.

I have an application whereby I am creating tabs in a TabNavigator dynamically. As you can see as per my codes I am basically having 5 tabs with specific names. Now I also have 5 mxml components with the same names as the tabs (that is the names of the mxml components are same as the tabs, that is Tab1,Tab2 etc. The non-dynamic way would be to use myTab1:Tab1 = new Tab1();myTab1:Tab2 = new Tab2); etc. Thus I will have to do this for each tab which I don't want.
What I want to do is to load the mxml components in each tab while I am looping through the Array. Hope I am clear enough.

    <?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" minWidth="955" minHeight="600">
        <fx:Script>
            <![CDATA[
                import mx.containers.VBox;
                import mx.controls.Label;
                import mx.events.FlexEvent;
                import spark.components.NavigatorContent;


                protected function tabNavigator_creationCompleteHandler(event:FlexEvent):void
                {

                    var superModules:Array =["Tab1","Tab2","Tab3","Tab4","Tab5"];
                    for each (var superModule in superModules)
                    {
                        var myNavigatorContent:NavigatorContent = new NavigatorContent();
                        myNavigatorContent.percentHeight = 100;
                        myNavigatorContent.percentWidth = 100;
                        myNavigatorContent.label = superModule;
                        myNavigatorContent.addChild(superModule as DisplayObject);
                                        tabNavigator.addChild(myNavigatorContent);

                    }   


                }

            ]]>
        </fx:Script>
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
        <mx:TabNavigator id="tabNavigator" width="100%" height="100%" creationComplete="tabNavigator_creationCompleteHandler(event)">
        </mx:TabNavigator>
    </s:Application>

  Can somebody help on achieving this. 
    Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素染倾城色 2024-12-18 14:54:48

是的,你可以做到。请在下面找到我的解决方案。

<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" minWidth="955" minHeight="600">
<fx:Script>
    <![CDATA[
        import mx.containers.VBox;
        import mx.controls.Label;
        import mx.core.IVisualElement;
        import mx.core.UIComponent;
        import mx.events.FlexEvent;

        import spark.components.NavigatorContent;

        /*The class "Tab1","Tab2" not compiled with the application 
        because the linker and the compiler do not add classes 
        that are not referenced in the code.

        Even though you’ll never use the _dummyVarToAddTabToAppCompilation1 
        variable it is necessary to use that line to instruct the compiler 
        to include Tab1,Tab2 in the compilation.*/

        private var _dummyVarToAddTabToAppCompilation1:Tab1;
        private var _dummyVarToAddTabToAppCompilation2:Tab2;

        protected function tabNavigator_creationCompleteHandler(event:FlexEvent):void
        {

            var superModules:Array =["Tab1","Tab2"];
            for each (var superModule in superModules)
            {
                var myNavigatorContent:NavigatorContent = new NavigatorContent();
                myNavigatorContent.percentHeight = 100;
                myNavigatorContent.percentWidth = 100;
                myNavigatorContent.label = superModule;
                // Convert class name to Class object.
                var cls:Class = getDefinitionByName(superModule) as Class;
                // Create a new instance of the class.
                var instance:UIComponent = new cls();
                myNavigatorContent.addElement(instance);
                tabNavigator.addChild(myNavigatorContent);
            }   
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:TabNavigator id="tabNavigator" width="100%" height="100%" creationComplete="tabNavigator_creationCompleteHandler(event)">
</mx:TabNavigator>

Yes you can do it.Please find my solution below.

<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" minWidth="955" minHeight="600">
<fx:Script>
    <![CDATA[
        import mx.containers.VBox;
        import mx.controls.Label;
        import mx.core.IVisualElement;
        import mx.core.UIComponent;
        import mx.events.FlexEvent;

        import spark.components.NavigatorContent;

        /*The class "Tab1","Tab2" not compiled with the application 
        because the linker and the compiler do not add classes 
        that are not referenced in the code.

        Even though you’ll never use the _dummyVarToAddTabToAppCompilation1 
        variable it is necessary to use that line to instruct the compiler 
        to include Tab1,Tab2 in the compilation.*/

        private var _dummyVarToAddTabToAppCompilation1:Tab1;
        private var _dummyVarToAddTabToAppCompilation2:Tab2;

        protected function tabNavigator_creationCompleteHandler(event:FlexEvent):void
        {

            var superModules:Array =["Tab1","Tab2"];
            for each (var superModule in superModules)
            {
                var myNavigatorContent:NavigatorContent = new NavigatorContent();
                myNavigatorContent.percentHeight = 100;
                myNavigatorContent.percentWidth = 100;
                myNavigatorContent.label = superModule;
                // Convert class name to Class object.
                var cls:Class = getDefinitionByName(superModule) as Class;
                // Create a new instance of the class.
                var instance:UIComponent = new cls();
                myNavigatorContent.addElement(instance);
                tabNavigator.addChild(myNavigatorContent);
            }   
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:TabNavigator id="tabNavigator" width="100%" height="100%" creationComplete="tabNavigator_creationCompleteHandler(event)">
</mx:TabNavigator>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文