事件元数据:使用静态变量作为“名称”属性?
我想使用事件元数据标签来显示我的控件将调度什么类型的事件。语法如下所示:
<fx:Metadata>
[Event(name="eventName", type="MyEvent")]
</fx:Metadata>
在 Flex/Actionscript 中定义定义事件名称的静态变量似乎是最佳实践,如下所示:
public class MyEvent extends Event
{
public static const EVENT_NAME:String = "eventName";
// Other stuff..
}
这是一个很好的实践,因为事件名称可以轻松更改,而不必在整个代码中进行修改。所以我的问题是:有什么方法可以在我的元数据事件标记中使用这个静态常量吗?我似乎无法做这样的事情:
<fx:Metadata>
[Event(name="{MyEvent.EVENT_NAME}", type="MyEvent")]
</fx:Metadata>
我只是不知道执行此操作的正确语法,还是不可能?似乎如果有人决定更改 const,它只是要求很难找到错误,因为它在这里不是强类型的。提前致谢!
I want to use the Event metadata tags to show what types of event my control will dispatch. The syntax looks like:
<fx:Metadata>
[Event(name="eventName", type="MyEvent")]
</fx:Metadata>
It seems like a best practice in Flex/Actionscript to define static variables that define event names like so:
public class MyEvent extends Event
{
public static const EVENT_NAME:String = "eventName";
// Other stuff..
}
It's a great practice since the event name can change easily and not have to be modified throughout the code. So my question is: Is there any way to use this static const in my metadata event tag? I can't seem to do something like this:
<fx:Metadata>
[Event(name="{MyEvent.EVENT_NAME}", type="MyEvent")]
</fx:Metadata>
Am I just ignorant of the proper syntax to do this, or is it impossible? Seems like it's just asking for hard to find bugs if someone decides to change the const since it is not strongly typed here. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
坏消息是这是不可能的:(我梦想着这种可能性,但是......
The bad news is it is impossible :( I dream about this possibility but…
是的,这是不可能做到的,但是没有什么可以阻止您进行自己的运行时检查。通过调用类上的
describeType()
(在本例中)可以获得元数据。解析 xml 并根据常量检查值。如果出现问题,抛出错误或输出跟踪。它并不完美,但它会给你一些安全感yeah, this is impossible to do, but there's nothing stopping you from doing your own runtime checking. metadata is available by calling
describeType()
on the class (in this instance). parse the xml and check the values against your consts. if there's a problem, throw an error or output a trace. it's not perfect, but it'll give you some safety