flex 4从自定义组件中解除自定义事件
-
25-10-2019 - |
题
这类似于问的问题 这里. 。我正在派遣自定义事件“ Shopevent”,但我会遇到错误 “类型强制失败:无法将flash.events :: event@81ecb79转换为com.events.shopevent”
注意:错误是从父级自定义组件(第三代码段)丢弃的,我在那里添加了更多详细信息
这是我的自定义活动。请参阅第一个常数,我将其复制在自定义组件中的事件名称粘贴。
package com.events
{
import flash.events.Event;
public class ShopEvent extends Event
{
public static var MENU_SELECTED:String = "menuSelected";
public static var SUBMENU_SELECTED:String = "submenuSelected";
public static var ITEM_SELECTED:String = "itemSelected";
public static var NAV_NEXT:String = "navNext";
public static var NAV_PREVIOUS:String = "navPrevious";
public static var NAV_LAST:String = "navLast";
public static var NAV_FIRST:String = "navFirst";
public static var CLOSE:String = "close";
public var menuIdx:int;
//public var menuType:String;
public var menuId:int;
public var menuName:String;
public var itemId:int;
public function ShopEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
自定义组件。检查元数据标签。该活动正确注册了
<?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="72" height="82"
mouseChildren="false"
creationComplete="init()"
click="onClick()"
buttonMode="true">
<s:layout>
<s:VerticalLayout horizontalAlign="center"/>
</s:layout>
<fx:Script>
<![CDATA[
import com.events.ShopEvent;
import mx.controls.Image;
public var menuId:int;
[Bindable]
public var menuText:String;
[Bindable]
public var bmp:Bitmap;
private function init():void{
//img.addChild(bmp);
}
private function onClick():void{
var e:ShopEvent = new ShopEvent(ShopEvent.MENU_SELECTED);
e.menuId = menuId;
e.menuName = menuText;
dispatchEvent(e);
}
]]>
</fx:Script>
<fx:Metadata>
[Event(name="menuSelected", type="com.events.ShopEvent")]
</fx:Metadata>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label text="{menuText}" fontWeight="bold" fontSize="12" width="44"/>
<mx:Image id = "img" width="57" height="47" source="{bmp}"/>
</s:Group>
父自定义组件。这是上述自定义组件的父组件。它听取了菜单视为的事件,它只是将事件路由到听众。检查Meatadata标签。事件注册是正确完成的。
但是,错误即将到来
menus[i].addEventListener( ShopEvent.MENU_SELECTED,function(e:ShopEvent):void{dispatchEvent(e);});
据我所知,我在代码中看不到任何问题。里面有什么问题吗?
更新
令人惊讶的是,如果我创建一个shopwevent的“新”实例将解决问题, 但是可悲的是,我需要关闭事件对象的所有属性. 。我希望这不是弹性的限制。
menus[i].addEventListener( ShopEvent.MENU_SELECTED,function(e:ShopEvent):void{dispatchEvent(new ShopEvent(ShopEvent.MENU_SELECTED));});
完成代码
<?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="720" height="605"
creationComplete="init()" xmlns:shop="UI.shop.*" xmlns:hasu="UI.shop.hasu.*"
>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Metadata>
[Event(name="navNext", type="com.events.ShopEvent")]
[Event(name="navPrevious", type="com.events.ShopEvent")]
[Event(name="menuSelected", type="com.events.ShopEvent")]
[Event(name="submenuSelected", type="com.events.ShopEvent")]
[Event(name="itemSelected", type="com.events.ShopEvent")]
[Event(name="close", type="com.events.ShopEvent")]
</fx:Metadata>
<fx:Script>
<![CDATA[
import com.events.ShopEvent;
private const MAX_SLOTS:int = 6;
public var menus:Vector.<ShopMenuItemView>;
public var itemSlots:Vector.<ShopItemSlotView> = new Vector.<ShopItemSlotView>(MAX_SLOTS);
private function init():void{
trace("BEGIN:UI.shop.hasu.ShopView.init");
initSlots();
}
private function initSlots():void{
for (var i:int = 0;i<itemSlots.length;i++){
var slot:ShopItemSlotView = new ShopItemSlotView();
itemSlots[i] = slot;
itemSlotsContainer.addElement(slot);
}
}
public function initMenus():void{
trace("BEGIN:UI.shop.hasu.ShopView.initMenus");
for (var i:int = 0;i < menus.length;i++){
menuContainer.addElement(menus[i]);
menus[i].addEventListener(ShopEvent.MENU_SELECTED,function(e:ShopEvent):void{dispatchEvent(e);});
//menus[i].addEventListener( ShopEvent.MENU_SELECTED,function(e:ShopEvent):void{dispatchEvent(new ShopEvent(ShopEvent.MENU_SELECTED));});
}
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout />
</s:layout>
<s:VGroup name="top">
<hasu:ShopPlayerAttributesView id="attribsComp"/>
<s:Group id="menuContainer" name="menus">
<s:layout>
<s:HorizontalLayout />
</s:layout>
</s:Group>
</s:VGroup>
<s:Group>
<s:layout>
<s:HorizontalLayout />
</s:layout>
<s:Button label="<" />
<s:Group id = "itemSlotsContainer" name="items">
<s:layout>
<s:TileLayout requestedColumnCount="3" requestedRowCount="3"/>
</s:layout>
</s:Group>
<s:Button label=">" />
</s:Group>
</s:Group>
解决方案
您必须覆盖自定义事件类别的clone()方法。在传播过程中可以克隆事件多次。
其他提示
您必须返回活动类的新构造函数,例如:
return new ShopEvent(type,...); //in the clone() method;
这 clone()
方法通过设置类型属性和克隆中的任何新属性来返回事件对象的克隆副本。通常,您定义 clone()
返回使用新操作员创建的事件实例的方法。
您要派遣的事件来自类型Flash.events.event,而您的听众期望的事件来自类型:com.events.shopevent。
这仅仅是错误消息的含义。
不隶属于 StackOverflow