Flex 在另一个视图上执行函数
我有一个 headerbar.mxml 当用户在我的应用程序中 swipes_down 时显示。 headerbar.mxml 包含一个按钮组件,我想在主应用程序窗口中运行擦除()。主要应用程序是一个包含擦除()的绘图应用程序。我只是不知道如何从另一个 mxml 视图文件调用函数。我认为它会像 click="{mainwindow.drawPanel.erase()}";
编辑:
protected function onColorChange(event:List):void{
appHome.drawArea.erase();
}
I have a headerbar.mxml that is displayed when user swipes_down in my app. The headerbar.mxml contains a button component I want to run an erase() in the main application window. The main application is a drawing app that contains an erase(). I just don't know how to call a function from another mxml view file. I thought it would be something like click="{mainwindow.drawPanel.erase()}";
EDIT:
protected function onColorChange(event:List):void{
appHome.drawArea.erase();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在另一个视图(又称组件)中运行功能,这在很大程度上取决于架构。听起来您想在父级中运行一个函数。在这种情况下,“正确封装”方法是从 component1 分派事件;监听组件1的父级中的事件;并从事件监听器执行该函数。
因此,在 headerbar 的父级中的某个位置添加事件侦听器:
如果是 ActionSCript 3 组件,我可能会将其添加到构造函数中;如果是 MXML 组件,我可能会将其添加到预初始化事件处理程序中。 “父”组件还需要侦听器函数:
当单击 headerbar.mxml 中的按钮组件时,这会触发 headerbar 内部的单击事件处理程序,该处理程序需要分派该事件,如下所示:
一切都应该神奇地工作。如果该函数不在父级的直接子级中,您可能必须冒泡该事件。
如果您不关心封装,也可以直接访问父级。所以你的标题栏组件会这样做:
它简单直接,并且应该可以工作,但从维护的角度来看被认为是非常糟糕的做法。
To run a function in another view (AKA Component) It depends largely on the architecture. It sounds like you want to run a function in your parent. In which case the 'encapsulation proper' method is to dispatch an event from component1; listen for the event in component1s parent; and execute the function from the event listener.
So, somewhere in headerbar's parent, add the event listener:
I'd probably add this in the constructor if an ActionSCript 3 component, or in a preinitialize event handler if an MXML Component. The 'parent' component will also need the listener function:
When the clicks the button component in headerbar.mxml and this fires off a click event handler inside of headerbar, which needs to dispatch the event, like this:
And everything should magically work. You may have to bubble the event if the function is not inside a direct child of the parent.
If you don't care about encapsulation, you can also just access the parent directly. So your header bar component would do this:
It's simple and straight forward, and should work, but is considered horribly bad practice from a maintenance stand point.