Actionscript 3 Flex 4.6:对列表中的按钮执行操作

Actionscript 3 Flex 4.6:对列表中的按钮执行操作,actionscript-3,apache-flex,flex4.5,mxml,Actionscript 3,Apache Flex,Flex4.5,Mxml,我是Flex的初学者。我的问题何时将按钮添加到列表中,我将按钮放在itemrender中,但是navigator.pushView(views.Listde,ProblemsList.selectedItem)的操作; 在“访问未定义的属性导航器”时出错 代码: <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spar

我是Flex的初学者。我的问题何时将按钮添加到列表中,我将按钮放在itemrender中,但是
navigator.pushView(views.Listde,ProblemsList.selectedItem)的操作;
在“访问未定义的属性导航器”时出错

代码:

<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
    <![CDATA[
        import views.ButList;

        protected function button1_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub
            navigator.pushView(views.Listde,ProblemsList.selectedItem);
        }

    ]]>
</fx:Script>

<s:Group width="100%" height="100%" styleName="PCS.css">
    <s:HGroup width="100%" height="100%" gap="2" verticalAlign="middle" horizontalAlign="center" paddingBottom="5" paddingLeft="5"
              paddingRight="5" paddingTop="5">
        <s:HGroup width="30%" height="100%" verticalAlign="middle" horizontalAlign="center">
            <s:Label text="{data._AddedDate}" textAlign="left" verticalAlign="bottom" width="100%" height="100%"/>
        </s:HGroup>
        <s:VGroup width="50%" height="100%" verticalAlign="middle" horizontalAlign="right">
            <s:BitmapImage source="images/{data.image}"/>
            <s:TextArea editable="false" text="{data.description}"/>
            <s:Label text="{data.price}"/>
            <s:Button label="s" click="button1_clickHandler(event)"/>                   
        </s:VGroup>
    </s:HGroup>
</s:Group>

如果任何解决方案通过列表或任何组件(而不是列表)来解决此问题,则我希望按钮导航到具有选定项的视图,并且列表的更改也导航到具有选定项的另一个视图,则列表的更改工作成功,但无法使用导航器定义按钮操作


提前感谢您的帮助。

使用data属性创建自定义事件,并在单击按钮时从项目呈现器中分派事件或从项目呈现器中分派列表更改事件

package myEvents
{
import flash.events.Event;

public class CustomEvent extends Event
{
    public function CustomEvent(type:String, 
        data:Object=null) {
            // Call the constructor of the superclass.
            super(type);

            // Set the new property.
            this.data = data;
    }

    // Define static constant.
    public static const MY_BUTTON_CLICKED:String = "myButtonClicked";

    // Define a public variable to hold the state of the enable property.
    public var data:Object;

    // Override the inherited clone() method.
    override public function clone():Event {
        return new CustomEvent(type, data);
    }
  }
}
<?xml version="1.0" encoding="utf-8"?>

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
title="MY View">
    <s:layout>
        <s:VerticalLayout paddingTop="10"/>
    </s:layout>

    <fx:Script>
        <![CDATA[
            import spark.events.IndexChangeEvent;
            import views.ButList;

            protected function myList_changeHandler(event:IndexChangeEvent):void {
                navigator.pushView(views.Listde,myList.selectedItem);
            }

        ]]>
    </fx:Script>

    <s:Label text="Select an view"/>
    <s:List id="myList"
        width="100%" height="100%"
        labelField="firstName" itemRenderer="MyItemRenderer"
        change="myList_changeHandler(event)">
        <s:ArrayCollection>
            <fx:Object description="Fruits" image="assets/icons/1.png" _AddedDate="15/05/14" price="154"/>
            <fx:Object description="New Item" image="assets/icons/2.png" _AddedDate="15/05/14" price="154"/>
             <fx:Object description="Doll" image="assets/icons/3.png" _AddedDate="15/05/14" price="154"/>
        </s:ArrayCollection>
    </s:List>
</s:View>
MyItemRenderer.mxml

<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
    <![CDATA[
        import views.ButList;

        protected function button1_clickHandler(event:MouseEvent):void
        {
            // dispatch a custom
            (this.owner as List).dispatchEvent(new CustomEvent(CustomEvent.MY_BUTTON_CLICKED, data));
            //navigator.pushView(views.Listde,ProblemsList.selectedItem);
        }

    ]]>
</fx:Script>

<s:Group width="100%" height="100%" styleName="PCS.css">
    <s:HGroup width="100%" height="100%" gap="2" verticalAlign="middle" horizontalAlign="center" paddingBottom="5" paddingLeft="5"
              paddingRight="5" paddingTop="5">
        <s:HGroup width="30%" height="100%" verticalAlign="middle" horizontalAlign="center">
            <s:Label text="{data._AddedDate}" textAlign="left" verticalAlign="bottom" width="100%" height="100%"/>
        </s:HGroup>
        <s:VGroup width="50%" height="100%" verticalAlign="middle" horizontalAlign="right">
            <s:BitmapImage source="images/{data.image}"/>
            <s:TextArea editable="false" text="{data.description}"/>
            <s:Label text="{data.price}"/>
            <s:Button label="s" click="button1_clickHandler(event)"/>                   
        </s:VGroup>
    </s:HGroup>
</s:Group>

您的视图类

<?xml version="1.0" encoding="utf-8"?>

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
title="MY View">
<s:layout>
    <s:VerticalLayout paddingTop="10"/>
</s:layout>

<fx:Script>
    <![CDATA[
        import spark.events.IndexChangeEvent;

        private function onListCreationComplete_Handler():void
        {
            myList.addEventListener(CustomEvent.MY_BUTTON_CLICKED, onItemRendererButtonClicked);
        }

        protected function onItemRendererButtonClicked(event:CustomEvent):void {
            var data:Object = event.data;
            navigator.pushView(views.Listde,data);//myList.selectedItem
        }

    ]]>
</fx:Script>

<s:Label text="Select an view"/>
<s:List id="myList"
    width="100%" height="100%"
    labelField="firstName" itemRenderer="MyItemRenderer"
    change="myList_changeHandler(event)" creationComplete="onListCreationComplete_Handler()">
    <s:ArrayCollection>
        <fx:Object description="Fruits" image="assets/icons/1.png" _AddedDate="15/05/14" price="154"/>
        <fx:Object description="New Item" image="assets/icons/2.png" _AddedDate="15/05/14" price="154"/>
         <fx:Object description="Doll" image="assets/icons/3.png" _AddedDate="15/05/14" price="154"/>
    </s:ArrayCollection>
</s:List>
</s:View>


我希望这能对你有所帮助。手机列表和项目渲染器有很多例子。

基于你的
项目渲染器
,你只需在
视图
中收听
索引交换事件
。在这种情况下,你根本不需要一个单独的按钮-整个
项目渲染器
可以充当bu塔顿

package myEvents
{
import flash.events.Event;

public class CustomEvent extends Event
{
    public function CustomEvent(type:String, 
        data:Object=null) {
            // Call the constructor of the superclass.
            super(type);

            // Set the new property.
            this.data = data;
    }

    // Define static constant.
    public static const MY_BUTTON_CLICKED:String = "myButtonClicked";

    // Define a public variable to hold the state of the enable property.
    public var data:Object;

    // Override the inherited clone() method.
    override public function clone():Event {
        return new CustomEvent(type, data);
    }
  }
}
<?xml version="1.0" encoding="utf-8"?>

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
title="MY View">
    <s:layout>
        <s:VerticalLayout paddingTop="10"/>
    </s:layout>

    <fx:Script>
        <![CDATA[
            import spark.events.IndexChangeEvent;
            import views.ButList;

            protected function myList_changeHandler(event:IndexChangeEvent):void {
                navigator.pushView(views.Listde,myList.selectedItem);
            }

        ]]>
    </fx:Script>

    <s:Label text="Select an view"/>
    <s:List id="myList"
        width="100%" height="100%"
        labelField="firstName" itemRenderer="MyItemRenderer"
        change="myList_changeHandler(event)">
        <s:ArrayCollection>
            <fx:Object description="Fruits" image="assets/icons/1.png" _AddedDate="15/05/14" price="154"/>
            <fx:Object description="New Item" image="assets/icons/2.png" _AddedDate="15/05/14" price="154"/>
             <fx:Object description="Doll" image="assets/icons/3.png" _AddedDate="15/05/14" price="154"/>
        </s:ArrayCollection>
    </s:List>
</s:View>


navigator以视图的名称导航到另一个视图OK,那么flex也不知道它是什么,这个导航器在哪里?它是什么类型?它的类型是spark.components.view.navigator():ViewNavigator,我不知道如何定义它的类型我尝试导入spark.components,但在我键入navigator时仍未定义声明的类型是navigateToURL(),NavigationUnit和NavigatorContent仅没有navigator。它找不到navigator,因为它在另一个类中定义,您需要一个事件来检索navigator,或者如果navigator位于主应用程序类中,请使用FlexGlobals.topLevelApplication.navigator您可以使用FlexGlobals访问主类,但Adobe建议您不要使用该事件使用它。+1回答很好。请注意,您也可以将内置的
DataEvent
与列表的
selectedIndex
一起使用,而不是使用带有
selectedItem
的自定义事件。谢谢,是的,我使用了myList.selectedItem。