Apache flex 我能';t将自定义事件从一个模块分派到另一个模块,因为它给出TypeError:Error#1034:Type强制失败:
我正在尝试将自定义事件从一个flex模块分派到另一个flex模块 发送事件的代码如下所示Apache flex 我能';t将自定义事件从一个模块分派到另一个模块,因为它给出TypeError:Error#1034:Type强制失败:,apache-flex,actionscript-3,events,event-dispatching,Apache Flex,Actionscript 3,Events,Event Dispatching,我正在尝试将自定义事件从一个flex模块分派到另一个flex模块 发送事件的代码如下所示 Application.application.Destination.child.dispatchEvent( new AlgoEvent(AlgoEvent.GETFROMPARENT_LOCAL_EVENT)); 这里AlgoEvent是一个自定义事件 另一方面,捕获和处理事件的模块有以下代码: public function sendParametersToChild(e:AlgoEve
Application.application.Destination.child.dispatchEvent(
new AlgoEvent(AlgoEvent.GETFROMPARENT_LOCAL_EVENT));
这里AlgoEvent是一个自定义事件
另一方面,捕获和处理事件的模块有以下代码:
public function sendParametersToChild(e:AlgoEvent):void
{
//some codes
}
但是当语句Application.Application.Destination.child.dispatchEvent(新的AlgoEvent(AlgoEvent.GETFROMPARENT_LOCAL_EVENT))当调试器执行代码>时,将出现以下运行时异常:
TypeError: Error #1034: Type Coercion failed: cannot convert resources.events::AlgoEvent@4182239 to resources.events.AlgoEvent.
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9298]
at components::Destination/sendTimeToChild()[E:\FlexProjects\MyApp\src\components\Destination.mxml:99]
at components::Destination/updateParameters()[E:\FlexProjects\MyApp\src\components\Destination.mxml:206]
at components::Destination/__CreateBasketButton_click()[E:\FlexProjects\MyApp\src\components\Destination.mxml:558]
我无法确定这里出了什么问题。
请帮忙解决这个问题
这是我的活动课
public class AlgoEvent extends Event
{
public static const GETFROMPARENT_LOCAL_EVENT:String = "getfromparent_local";
private var eventType:String;
public function AlgoEvent(eventType:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(eventType,bubbles,cancelable);
this.eventType=eventType;
}
}
调试时,UIComponent类的此函数出错
override public function dispatchEvent(event:Event):Boolean
{
if (dispatchEventHook != null)
dispatchEventHook(event, this);
return super.dispatchEvent(event);
}
例外,这一行给出了错误:dispatchEventHook(事件,this)代码>Mate框架负责所有这些。
它为应用程序中的所有模块提供了一个全局事件总线。
在主应用程序中导入AlgoEvent
类并创建对它的引用
import resources.events.AlgoEvent;
private var dummyEvent: AlgoEvent;
以下是对此的一些解释:
如果自定义事件没有任何特殊的事件属性,则可以使用标准的event
类解决问题
dispatchEvent(new Event(AlgoEvent.GETFROMPARENT_LOCAL_EVENT));
尝试重写自定义事件AlgoEvent中的clone()方法。
将以下代码添加到AlgoEvent.as类中,然后重试:
重写公共函数clone():事件{
返回新事件(eventType、气泡、可取消);
}
嗯
您的自定义事件类应如下所示:
public class AlgoEvent extends Event
{
public static const GETFROMPARENT_LOCAL_EVENT:String = "getfromparent_local";
public function AlgoEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
};
override public function clone():AlgoEvent
{
return new AlgoEvent(type, bubbles, cancelable);
};
};
您应该使用事件的继承类型属性,而不是创建新属性
另外,UIComponent
有自己的dispatchEvent
方法,因此您不必创建自己的方法,只要它的工作方式与继承的方法不同
问候,,
Robsplash先生提出了一个对我有效的解决方案:
尝试让主应用程序知道Custum事件(在我的例子中是Algo事件)类
即在主应用程序中导入它并创建它的变量
主要原因是>>当我们尝试使用事件调度在模块之间进行通信时,发生的情况是:模块在运行时加载,但像事件类这样的类在运行时链接到模块。。
但事件类是在加载模块之前编译的
应用程序在编译时定义一个Custum事件类,模块在发布时定义自己的Custum事件类。然后,当应用程序运行时,应用程序中调度的Custum事件类与模块中的不匹配
主权财富基金
对于导致此错误的问题,可以检查链接:
而且
好的,必须指出,从架构的角度来看,您所做的是错误的。调用Application.Application有很多不好的原因,特别是当您开始进入显示树时。第二次任何子项发生更改时,您的构建现在已中断,直到运行时您才知道这一点,因为它是一个模块
您需要的是一个应用程序框架。一种在不降低可维护性的情况下增加复杂性的方法。有很多,但我个人最喜欢的是。我已经在许多非常大的项目中使用过它,并取得了很大的成功。您现在试图解决的问题是,将一个事件分派给另一个模块侦听它,这非常简单(大约需要3行代码)
我建议您和我的一起查看。我在调度时遇到了相同的问题,解决了覆盖两个函数的问题:
override public function clone():Event
{
return new AlgoEvent(type, bubbles, cancelable);
}
override public function toString():String
{
return formatToString("AlgoEvent","type"","bubbles","cancelable","eventPhase");
}
希望有帮助:)这与Orbeon有什么关系?(我是在你给这个问题贴上标签的时候问的。)但我以前也用过同样的机制,而且效果很好。。引发的事件的类型仅为AlgoEvent.GETFROMPARENT_LOCAL_event,但在处理原型og的另一端,事件处理程序即公共函数sendParametersToChild(e:AlgoEvent):void给出错误:e:AlgoEvent是此错误的根本原因。。如果我使用e:Event而不是e:AlgoEvent,它会工作得很好。。但是我只希望它是e:AlgoEvent,因为我用AlgoEvent附加了一些数据,以便在另一端访问它。你能分享一些代码吗。使用相关的代码片段更新您的问题。添加了代码,但没有效果。。我无法理解此处要求的转换类型。。TypeError:错误#1034:类型强制失败:无法转换资源。事件::AlgoEvent@63690b1到resources.events.AlgoEvent。。。。我正在抛出相同的Algo事件,并在处理程序中捕获相同的事件。@user672468:如果您正在使用Firefox,那么您应该清理浏览器缓存以确保更改生效。谢谢..我通过您的“尝试让主应用程序知道AlgoEvent类”方法获得了解决方案。。我刚刚在我的主应用程序中导入了它。。但现在我试着在主应用程序中简单地使用一个Algo事件变量。。成功了!!!非常感谢……好的。。因为我需要一些数据与事件一起发送,所以我只能创建一个cusum事件的instatnce。。