我无法将自定义事件从一个模块分派到另一个模块,因为它给出 TypeError: Error #1034: Type Coercion failed:

发布于 2024-10-26 08:32:44 字数 1976 浏览 1 评论 0原文

我正在尝试将自定义事件从一个弹性模块分派到另一个。

调度事件的代码如下

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(new 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]

我无法确定这里出了什么问题。 请帮助解决这个问题

这是我的Event类

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); 
}

Excaxtly这​​一行给出了错误:dispatchEventHook(event, this);

I am trying to dispatch a custom event from one flex module to another.

The code which dispatch the event is as below

Application.application.Destination.child.dispatchEvent(
    new AlgoEvent(AlgoEvent.GETFROMPARENT_LOCAL_EVENT));

here AlgoEvent is a custom event

on the other side the module which catches and handles the event has this code:

public  function sendParametersToChild(e:AlgoEvent):void
{
    //some codes
}

but when the statement Application.application.Destination.child.dispatchEvent(new AlgoEvent(AlgoEvent.GETFROMPARENT_LOCAL_EVENT)); is executed the debugger give the following run time exception:

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]

I am not able to identify what is going wrong here.
Please help to solve this problem

This is my Event class

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;
    }
}

While debugging am getting error in this funcion of UIComponent class

override public function dispatchEvent(event:Event):Boolean
{
    if (dispatchEventHook != null)
        dispatchEventHook(event, this);

    return super.dispatchEvent(event); 
}

Excaxtly this line gives the error: dispatchEventHook(event, this);

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

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

发布评论

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

评论(7

扬花落满肩 2024-11-02 08:32:44

在主应用程序中导入 AlgoEvent创建对其的引用。

import resources.events.AlgoEvent;
private var dummyEvent: AlgoEvent;

对此的一些解释可以在这里找到:模块域

如果您的自定义事件不包含任何特殊事件属性,您可以使用标准 Event 类来解决您的问题。

dispatchEvent(new Event(AlgoEvent.GETFROMPARENT_LOCAL_EVENT));

Import the AlgoEvent class in the main application and create a reference to it.

import resources.events.AlgoEvent;
private var dummyEvent: AlgoEvent;

Some explanations for this could be found here: Module domains

If your custom event doesn't carry any special event properties you could workaround your problem by using the standard Event class.

dispatchEvent(new Event(AlgoEvent.GETFROMPARENT_LOCAL_EVENT));
◇流星雨 2024-11-02 08:32:44

我在调度时遇到了同样的问题,解决了覆盖两个函数:

override public function clone():Event
{
    return new AlgoEvent(type, bubbles, cancelable);
}

override public function toString():String
{
        return formatToString("AlgoEvent","type"","bubbles","cancelable","eventPhase");
}

希望它能有所帮助:)

I had the same problem when dispatching, solved overriding two functions:

override public function clone():Event
{
    return new AlgoEvent(type, bubbles, cancelable);
}

override public function toString():String
{
        return formatToString("AlgoEvent","type"","bubbles","cancelable","eventPhase");
}

hope it helps out :)

-小熊_ 2024-11-02 08:32:44

Mr.splash 提出了一个对我有用的解决方案:

尝试让主应用程序知道 Custum Event(在我的例子中是 Algo Event)类。

即在主应用程序中导入它并创建它的变量..

它的工作原理是>>当我们尝试使用事件调度在模块之间进行通信时发生的情况是:模块在运行时加载,但是像事件类这样的类在运行时链接到模块。
但事件类是在模块加载之前编译的。

应用程序在编译时定义了一个 Custum 事件类,模块在发布时定义了自己的 Custum 事件类。然后,当应用程序运行时,应用程序中调度的 Custum 事件类与模块中的事件类不匹配
swf。

对于导致此错误的问题,可以检查链接:

http://www .kirupa.com/forum/showthread.php?t=320390

以及

http://www.jeffdepascale.com/index.html php/flash/custom-events-in-loaded-swf-files/

Mr. splash suggested a solution which worked fro me:

Try to make the Custum Event (Algo Event in my case) class known to the main application.

I.e import it in the main application and create a variable of it..

And it works for a main reason>>when we try to communicate betwwen the modules using event dispatching what happens is :the modules are loaded at the run time but the classes like event classes are linked to the modules at the run time..
But the Event class is compiled before the modules are loaded..

application defines a Custum Event Class at compile time, and the module defines its own Custum Event Class when it is published. Then when the application is run, the Custum Event Class dispatched in the application doesn't match the one in the module
swf.

For the problem which is causing this error one can check the link:

http://www.kirupa.com/forum/showthread.php?t=320390

and also

http://www.jeffdepascale.com/index.php/flash/custom-events-in-loaded-swf-files/

仅一夜美梦 2024-11-02 08:32:44

Mate 框架负责这一切。
它为您的应用程序中的所有模块提供了一个全局事件总线。
http://mate.asfusion.com/

Mate framework takes care of all this.
It gives you a global event bus, for all modules in your app.
http://mate.asfusion.com/

苹果你个爱泡泡 2024-11-02 08:32:44

尝试重写您的自定义事件 AlgoEvent 中的clone()方法。
将以下代码添加到您的 AlgoEvent.as 类中并尝试:

重写公共函数clone():Event{
返回新的 AlgoEvent(eventType,bubbles,cancelable);
}
HTH。

Try to override the clone() method in your customized Event,AlgoEvent.
Add the following code to your AlgoEvent.as class and try:

override public function clone():Event{
return new AlgoEvent(eventType,bubbles,cancelable);
}
HTH.

许仙没带伞 2024-11-02 08:32:44

您的自定义事件类应如下所示:

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 方法,因此您不必创建自己的方法 - 仅当它的工作方式与继承的方法不同时。

问候,

Your custom Event class should look like this:

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);
    };
};

You should use the Event's inherited type property instead of creating a new one.

Also, the UIComponent has it's own dispatchEvent method, so you don't have to create your own - only if it works differently to the inherited one.

Regards,
Rob

疑心病 2024-11-02 08:32:44

好吧,必须说,从架构的角度来看,您所做的事情是错误的。由于多种原因,调用 Application.application 是不好的,特别是当您开始沿着显示树向下移动时。第二个任何子项发生更改时,您的构建现在就会损坏,并且直到运行时您才会知道这一点,因为它是一个模块。

您需要的是一个应用程序框架。一种增加复杂性而不降低可维护性的方法。有很多,但我个人最喜欢的是欧芹。我已经在许多非常大的项目中使用它并取得了巨大的成功。您现在正在尝试解决的问题是,在另一个模块侦听该事件时分派一个事件,这一问题非常简单(只需大约 3 行代码即可完成)。

我建议您仔细阅读它以及我的演示文稿欧芹简介

Okay, it must be said that what you're doing, from an architectural standpoint, is wrong. Calling Application.application is bad for so many reason, especially if you're then starting to go down the display tree. The second any of the children changes, your build is now broke, and you won't know that until runtime because it's a module.

What you need is an application framework. A way to increase complexity without decreasing maintainability. There are many out there, but my personal favorite is Parsley. I've used it on many very large projects with much success. The problem you're trying to solve right now, dispatching one event where the other module listens for it, is extremely trivial (can be done in about 3 lines of code).

I recommend you look it over as well as my presentation on an introduction to parsley.

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