在 Flex 中不使用 xml 模式调用调度事件的其他方法
我想知道是否有方法可以将dispatchEvents 与自定义事件函数调用一起使用,而无需实际以xml 形式实例化该函数,而是在脚本中这样做。我想做的是,让开发人员使用我的自定义组件按钮,但将他/她自己的函数添加到已添加到我自己的单击事件调用中的函数中。因此,就像他/她想添加该点击事件自己的验证功能一样。我的一种方法是在 xml 模式中的组件中创建一个实例。例如,我有它:
我有一个名为 DialogWindow.mxml 的 mxml 组件,
<mx:Metadata>
[Event(name="cancBtnClick", type="flash.events.Event")]
[Event(name="okBtnClick", type="flash.events.Event")]
</mx:Metadata>
在 DialogWindow.mxml 的脚本中,我创建了一个带有名为 cancelAction(event) 的单击事件的按钮;
_buttonCancel = new Button();
_buttonCancel.label = "Cancel";
_buttonCancel.addEventListener("click", cancelAction);
_buttonCancel.enabled = true;
在“cancelAction”函数中,我将dispatchEvent 添加到cancBtnClick,因为这将是用户定义的函数。
protected function cancelAction(event:Event):void {
dispatchEvent(new Event("cancBtnClick"));
PopUpManager.removePopUp(this);
}
当从另一个组件派生组件 DialogWindow.mxml 时,我可以使用此语句,其中 myFunction 取代了 cancelAction 函数中的 cancBtnClick。
// from TestButton.mxml
<cmp:DialogWindow cancBthClick="myFunction()" />
上面的方法工作正常,但是如果我想在脚本中使用派生类调用来调用它怎么办?比如
public var dialogW:IFlexDisplayObject = null;
(dialogW as DialogWindow).cancBtnClick = "myFunction"; // this is an instance of DialogWindow
尝试这个的时候,当然没有cancBtnClick。所以我知道我没有正确实施这一点。如果您知道我如何更改我的代码,以便我可以执行上述操作。我将不胜感激。先感谢您。
I wanted to know if there are ways to use dispatchEvents with custom event function calls without actually instantiate the function in an xml form but doing so in the script. What I am trying to do is, have a developer use my custom component button, but add his/her own function to what is already added to my own click event call. so just as a case he/she wants to add there own validation function of that click event. One way i have it is where i create an instance in my component within the xml schema. example, I have it where:
i have a mxml component called DialogWindow.mxml and inside I have
<mx:Metadata>
[Event(name="cancBtnClick", type="flash.events.Event")]
[Event(name="okBtnClick", type="flash.events.Event")]
</mx:Metadata>
and in my script in DialogWindow.mxml i created a button with a click event called cancelAction(event);
_buttonCancel = new Button();
_buttonCancel.label = "Cancel";
_buttonCancel.addEventListener("click", cancelAction);
_buttonCancel.enabled = true;
In the "cancelAction" function I add the dispatchEvent to cancBtnClick as this will be the userdefined function.
protected function cancelAction(event:Event):void {
dispatchEvent(new Event("cancBtnClick"));
PopUpManager.removePopUp(this);
}
When deriving the component DialogWindow.mxml from another component, I can use this statement where myFunction is taken place of cancBtnClick in cancelAction function.
// from TestButton.mxml
<cmp:DialogWindow cancBthClick="myFunction()" />
The above works fine, but what if i want to call it using derived class call in a script. such as
public var dialogW:IFlexDisplayObject = null;
(dialogW as DialogWindow).cancBtnClick = "myFunction"; // this is an instance of DialogWindow
When trying this, of course there is no cancBtnClick. So i know i am not implementing this correctly. If you know how I can change my code so i can do the above. I would be greatly appreciative. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要:
完成后删除侦听器是一个很好的做法。
You want:
It's good practice to also remove the listener when you are done.
在您的DialogWindow中,您可以定义属性
,然后按照您的意愿使用它
希望,它会对您有所帮助。
In your DialogWindow you can define property
And then use it as you wish
Hope, it will help you.