在 Flex 中不使用 xml 模式调用调度事件的其他方法

发布于 2024-10-19 02:02:25 字数 1427 浏览 2 评论 0原文

我想知道是否有方法可以将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 技术交流群。

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

发布评论

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

评论(2

逆光下的微笑 2024-10-26 02:02:25

您想要:

(dialogW as DialogWindow).addEventListener("cancBtnClick", yourFunction);

public function yourFunction(event:Event):void {
  // handle the event.
}

完成后删除侦听器是一个很好的做法。

(dialogW as DialogWindow).removeEventListener("cancBtnClick", yourFunction);

You want:

(dialogW as DialogWindow).addEventListener("cancBtnClick", yourFunction);

public function yourFunction(event:Event):void {
  // handle the event.
}

It's good practice to also remove the listener when you are done.

(dialogW as DialogWindow).removeEventListener("cancBtnClick", yourFunction);
圈圈圆圆圈圈 2024-10-26 02:02:25

在您的DialogWindow中,您可以定义属性

public function set cancBtnClick(func : Function) : void
{
    addEventListener("cancBtnClick", func);
}

,然后按照您的意愿使用它

(dialogW as DialogWindow).cancBtnClick = myFunction;

希望,它会对您有所帮助。

In your DialogWindow you can define property

public function set cancBtnClick(func : Function) : void
{
    addEventListener("cancBtnClick", func);
}

And then use it as you wish

(dialogW as DialogWindow).cancBtnClick = myFunction;

Hope, it will help you.

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