无法在 Flex 移动应用程序中分派自定义事件
我正在尝试从我遇到的示例中获取一些代码。大多数功能都可以工作,但当它尝试分派自定义事件时会失败。目前,尝试分派事件的代码位于处理 amf 远程处理的类内。
该示例中有此行用于调度:
Application.application.dispatchEvent(new
RemoteResultEvent(RemoteResultEvent.USER_UPDATE_COMPLETE,"test"));
但失败,因为它不知道 application.application 是什么“此行有多个标记: -访问未定义的属性应用程序”
我认为这是因为这不是为移动应用程序编写的。我尝试将调度程序更改为 EventDispatcher
EventDispatcher(
new RemoteResultEvent(RemoteResultEvent.USER_UPDATE_COMPLETE, "worked"));
但随后收到此错误:
TypeError: Error #1034: Type Coercion failed: 无法转换事件: :RemoteResultEvent@18337731 到 flash.events.EventDispatcher
这是自定义事件 RemoteResultEvent.as 中的代码:
package events
{
import flash.events.Event;
public class RemoteResultEvent extends Event {
public static var USER_UPDATE_COMPLETE:String = "UserUpdateComplete";
public var message:String;
public function RemoteResultEvent(eventType:String, message:String) {
super(eventType, false, false);
this.message = message;
}
}
}
我在黑暗中摸索,因为我是 Flex 新手。和这种类型的开发,所以我很可能会做一些非常愚蠢的事情,非常感谢
JaChNo
。
I am trying to get some code working from an example I came across. most of the functionality works but it is failing when it tries to dispatch a custom event. At the moment the code that is trying to dispatch the event is inside a class that handles amf remoting.
the example has this line in it for the dispatch:
Application.application.dispatchEvent(new
RemoteResultEvent(RemoteResultEvent.USER_UPDATE_COMPLETE,"test"));
but that fails as it does not know what application.application is "Multiple markers at this line:
-Access of undefined property application"
I assume that this is because this was not written for a mobile app. I tried changing the dispatcher to EventDispatcher
EventDispatcher(
new RemoteResultEvent(RemoteResultEvent.USER_UPDATE_COMPLETE, "worked"));
but I then get this error:
TypeError: Error #1034: Type Coercion failed: cannot convert events::RemoteResultEvent@18337731 to flash.events.EventDispatcher.
This is the code in the custom event RemoteResultEvent.as :
package events
{
import flash.events.Event;
public class RemoteResultEvent extends Event {
public static var USER_UPDATE_COMPLETE:String = "UserUpdateComplete";
public var message:String;
public function RemoteResultEvent(eventType:String, message:String) {
super(eventType, false, false);
this.message = message;
}
}
}
I am bumbling around in the dark as I am new to flex and this type of development so I could well be doing something really dumb. Any help would be gratefully received.
Thanks
JaChNo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,您似乎对事件调度感到困惑。
事件可以在任何扩展或具有 EventDispatcher 的 Flex 类中分派。大多数 Flex 组件(包括应用程序)都扩展了 EventDispatcher。要调度该事件,您只需执行以下操作即可:
这将从您当前的类中调度该事件。并不是所有 Flex UI 组件(包括那些用 MXML 制作的组件)都可以被视为一个类。
您想要做的是在主级应用程序上分派事件;这是一个可怕的封装破坏,但也是可行的。您必须将其转换为应用程序,这样您就不会获得通用对象。像这样:
此方法已弃用 自 Flex 4 起;并且您使用 FlexGlobals.topLevelApplication 相反:
您没有说,但您暗示了您处于移动项目中的事实。如果是这样,我不希望 mx 应用程序类可用,除非您明确地将带有 MX 组件的 SWC 添加到您的类中。您必须访问 Spark 应用程序,它没有 Application 属性。这可能就是您收到错误的原因。
请务必导入您要使用的正确应用程序:
有关 Spark 应用程序 类。
You seem confused about event dispatching in general.
Events can be dispatched in any Flex class that extends, or has a, EventDispatcher. Most Flex Components, including Application extend EventDispatcher. To dispatch the event, you are on the right track just do:
That will dispatch the event from your current class. Not that all Flex UI Components, including those made in MXML can be considered a class.
What you are trying to do is dispatch the event on the main level application; which is a horrible encapsulation breach, but doable. You have to cast it as an Application so you do not get a generic object. Like this:
This approach is deprecated since Flex 4; and you use the FlexGlobals.topLevelApplication instead:
You don't say, but you allude to the fact that you are in a Mobile Project. If so, I would not expect the mx Application class to be available unless you explicitly added the SWC w/ MX Components to your class. You'll have to access the Spark Application, which does not have an Application property. That could be why you are getting the error.
Be sure to import the proper application you want to use:
More info on Spark Application class.