Flex 在另一个视图上执行函数 [英] Flex executing a function on another view
问题描述
我有一个 headerbar.mxml,当用户在我的应用中使用 swipes_down 时会显示它.headerbar.mxml 包含一个按钮组件,我想在主应用程序窗口中运行擦除().主应用程序是一个包含擦除()的绘图应用程序.我只是不知道如何从另一个 mxml 视图文件调用函数.我认为它会像 click="{mainwindow.drawPanel.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()}";
protected function onColorChange(event:List):void{
appHome.drawArea.erase();
}
推荐答案
在另一个视图(AKA Component)中运行一个函数这很大程度上取决于架构.听起来你想在你的父母中运行一个函数.在这种情况下,封装正确"方法是从 component1 分派事件;侦听 component1 的父级中的事件;并从事件监听器执行函数.
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:
headerbarInstance.addEventListener('parentDoSomething', onHeaderBarToldMeTo);
如果是 ActionSCript 3 组件,我可能会在构造函数中添加它,如果是 MXML 组件,我可能会在预初始化事件处理程序中添加它.父"组件也需要监听器函数:
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:
protected function onHeaderBarToldMeTo(event:Event):void{
erase();
}
当单击 headerbar.mxml 中的按钮组件时,这会触发 headerbar 内的单击事件处理程序,该处理程序需要调度事件,如下所示:
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:
protected function onButtonInheaderbarClick(Event:Event):void{
dispatchEvent(new Event('parentDoSomething'));
}
一切都应该神奇地工作.如果函数不在父级的直接子级中,您可能需要冒泡事件.
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:
parent.erase();
它简单而直接,应该可以工作,但从维护的角度来看,它被认为是非常糟糕的做法.
It's simple and straight forward, and should work, but is considered horribly bad practice from a maintenance stand point.
这篇关于Flex 在另一个视图上执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!