Flex 手表可绑定属性其他类
我正在使用 Flex 4 创建一个应用程序。 当应用程序启动时,它会读取 XML 文件并填充对象。 .send() 调用是异步的,所以我想监听/监视这个填充的对象,当它完成时,为其他类分派一个事件,以便它们可以使用它。
package model{
public class LectureService extends HTTPService{
[Bindable]
private var _lecture:Lecture;
...
}
xml 被正确解析并加载到 Lecture 类的对象 Lecture 中。
如果我在 main.mxml 应用程序中使用 MXML 表示法,它工作正常(在异步请求后填充该对象时使用该对象):
<mx:Image id="currentSlide" source={lectureService.lecture.slides.getItemAt(0).path} />
但是,我有另一个 ActionScript 类,我无法侦听此调度(由 [Bindable])事件。
package components{
public class LectureSlideDisplay extends Image
{
public function LectureSlideDisplay()
{
super();
this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onChangeTest);
}
private function onChangeTest(e:PropertyChangeEvent):void {
trace('test');
}
我已经尝试过:
- 使用(如上)addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, methodName)。
- 尝试将 [Bindable] 更改为 [Bindalbe("nameEvent")] 并监听此内容,但什么也没有。
- 使用 CreateWatcher 方法,不起作用。
尝试查看该类的生成代码,但没有帮助我
if (this.hasEventListener("propertyChange")){ this.dispatchEvent(mx.events.PropertyChangeEvent.createUpdateEvent(this, "lecture", oldValue, value)); }
我如何监听并在另一个类中拥有填充的对象? 也许问题是我正在听另一个班级的课,但在这种情况下我该如何实现呢? 看来事件已发送,但我无法收听。
I'm creating an application using Flex 4.
When the app is started, it reads a XML file and populate objects. The .send() call is asynchronous, so I would want to listen/watch to this populated object, and when it has finished, dispatch an event for other classes, so they can use it.
package model{
public class LectureService extends HTTPService{
[Bindable]
private var _lecture:Lecture;
...
}
The xml is parsed correctly and loaded inside the object lecture of the class Lecture.
If I use the MXML notation in the main.mxml app, it works fine (the object is used when the it is populated after the async request):
<mx:Image id="currentSlide" source={lectureService.lecture.slides.getItemAt(0).path} />
BUT, I have another ActionScript class and I'm not able to listen to this dispatched (by [Bindable]) event.
package components{
public class LectureSlideDisplay extends Image
{
public function LectureSlideDisplay()
{
super();
this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onChangeTest);
}
private function onChangeTest(e:PropertyChangeEvent):void {
trace('test');
}
I have already tried:
- using (like above) addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, methodName).
- tried to change the [Bindable] to [Bindalbe("nameEvent")] and listen for this, nothing.
- using CreateWatcher method, doesn't work.
tried to have a look to the generated code of the class, but didn't help me
if (this.hasEventListener("propertyChange")){
this.dispatchEvent(mx.events.PropertyChangeEvent.createUpdateEvent(this, "lecture", oldValue, value));
}
How can I listen and have the populated object in another class?
Maybe the problem is that I'm listening from another class, but in this case how can I implement this?
It seems the event is dispatched, but I can't listen to it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于想要答案的人,我已经解决了更改 addEventListener 对象的问题。
使用是不正确的:
使用替代:
我已更改代码以在主应用程序 MXML 中侦听此事件,然后在处理程序方法内,调用组件的公共方法来使用新数据。
For who wants the answer, I have resolved changing the addEventListener object.
It is not right to use:
Use instead:
I have changed my code to listen to this event in the main app MXML, and then inside the handler method, call the public method of your components to use the new data.
仅通过扩展课程并不能解决所有问题。您确实应该研究命令用于您的 HTTP 请求。
更改属性事件在观察者内部使用,不会在整个组件中调度。您想要对 LectureSlideDisplay 执行的操作是覆盖源设置器。每次调用它时,都会将一个新值绑定到它:
您应该真正阅读 绑定是如何工作的。
You can't solve all your problems by just extending the class. You should really look into Commands for your HTTP requests.
The change property event is used internally for watchers and won't dispatch in the overall component. What you want to do for your
LectureSlideDisplay
is to override the source setter. Every time it is called, a new value is being binded to it:You should really read up on how Binding works.
考虑使用
BindingUtils
类。您可以找到文档此处。还有一些使用示例:one,< a href="http://www.popamihai.com/2009/07/flex/data-binding-in-flex-using-the-bindingutils-class/" rel="nofollow">两个。Consider to use
BindingUtils
class. You can found documentation here. And a couple of usage samples: one, two.