在 ActionScript / Flex 3 中将事件与类关联的最佳方法是什么?
ActionScript 3 / Flex 3 - 将自定义事件添加到类
假设我有以下事件:
import flash.events.Event;
public class SomeEvent extends Event
{
public static const EVENT_ACTION:String = "eventAction";
public function SomeEvent(type:String) {
super(type);
}
override public function clone():Event {
return new SomeEvent(this.type);
}
}
...和以下类:
public class SomeClass extends EventDispatcher
{
public function someFunction():void
{
dispatchEvent(new SomeEvent("eventAction"));
}
}
显示“SomeClass”抛出“SomeEvent”的最佳方式是什么? 我发现的唯一方法是用 [Event] 属性装饰“SomeClass”,如下所示:
[Event (name="eventAction", type="SomeEvent")]
这允许我实例化该类并通过执行以下操作添加事件侦听器:
var someClassInstance:SomeClass = new SomeClass();
someClassInstance.addEventListener(SomeEvent.EVENT_ACTION, mycallbackmethod);
有更好的方法吗? 将 [Event] 属性放在类上,后跟一些字符串文字感觉……是错误的。 先谢谢您的帮助!
ActionScript 3 / Flex 3 - Adding custom events to a class
Say I have the following Event:
import flash.events.Event;
public class SomeEvent extends Event
{
public static const EVENT_ACTION:String = "eventAction";
public function SomeEvent(type:String) {
super(type);
}
override public function clone():Event {
return new SomeEvent(this.type);
}
}
... and the following Class:
public class SomeClass extends EventDispatcher
{
public function someFunction():void
{
dispatchEvent(new SomeEvent("eventAction"));
}
}
What is the best way to show that 'SomeClass' throws 'SomeEvent'? The only way I have found is to decorate 'SomeClass' with the [Event] attribute, as follows:
[Event (name="eventAction", type="SomeEvent")]
This allows me to instantiate the class and add an event listener by doing this:
var someClassInstance:SomeClass = new SomeClass();
someClassInstance.addEventListener(SomeEvent.EVENT_ACTION, mycallbackmethod);
Is there a better way to do this? Putting the [Event] attribute on the class followed by some string literals just feels ... wrong. Thanks in advance for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你做对了。 目前,AS3 编译器仅允许元数据中使用字符串文字。 不能使用常量。
顺便说一下,Adobe 的公共 bug 数据库有一个允许在元数据中使用 ActionScript 常量的功能请求。 请随意投票。
You're doing it right. Currently, the AS3 compiler allows only string literals in metadata. Constants cannot be used.
By the way, Adobe's public bug database has a feature request to allow ActionScript constants in metadata. Feel free to vote for it.
我知道,我也有同样的感觉; 使用字符串文字似乎确实有点错误。 但据我所知,从我看过的所有文档、参加过的讲座以及阅读 Flex 源代码等来看,这确实是正确的方法。
I know, I feel the same way; using the string literals does seem a bit wrong somehow. But to the best of my knowledge, from all the documentation I've seen and the talks I've attended and in reading the Flex source code, etc., it does appear that's the proper way to do it.