当 Flex 3 中的公共属性发生更改时,如何调用方法?
如果我有一个 .mxml 文件,其中包含一个方法和一个公共属性,我可以在属性更改时执行该方法吗?
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Bindable]public var myProperty:MyType;
private function myMethod(myProperty):void
{
// Method to run every time myProperty changes
}
]]>
</mx:Script>
</mx:HBox>
在另一个 .mxml 文件中,我添加了这个 .mxml 文件,如下所示:
<viewComponents:MyViewComponent myProperty="{myVariable}" />
If I have a .mxml file that has a method in it and a public property, can I have the method execute whenever the property changes.
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Bindable]public var myProperty:MyType;
private function myMethod(myProperty):void
{
// Method to run every time myProperty changes
}
]]>
</mx:Script>
</mx:HBox>
In another .mxml file I have added this .mxml file like so:
<viewComponents:MyViewComponent myProperty="{myVariable}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于还没有人说过这一点,我将提出第二种方法。
Flex 框架中的每个属性都会分派一个名为 *property*Changed 之类的属性。其中 property 是要更改的属性的名称。正如其他人提到的,所述属性是使用 get set 方法实现的。像这样的事情:
可绑定元数据中指定的这些事件名称用于绑定目的。因此,您可以侦听此 myPropertyChanged 事件,而不是在集合内调用您的方法:
在代码的其他地方:
这对于您想要完成的任务来说可能有点过分了;或不。由于您没有详细说明您想要完成的任务,所以我不确定。
如果您的新功能以某种方式与 Flex 组件生命周期相关,例如更改显示或大小,您应该在生命周期方法中执行更改;不在你的 set 方法中。像这样的事情:
invalidate 方法将强制组件生命周期方法在下一个渲染事件期间重新运行,您可以在相关方法中使用如下代码:
Since no one said this yet, I'll propose a second approach.
Every property in the Flex Framework will dispatch a property named something like *property*Changed. where property is the name of the property to be changed. Said properties are implemented using get set methods as others have mentioned. Something like this:
These event name specified in the Bindable metadata is used for binding purposes. So, instead of calling your method inside the set, you could listen for this myPropertyChanged event:
And elsewhere in the code:
This may be overkill for what you're trying to accomplish; or not. Since you didn't go into specifics on what you were trying to accomplish, I'm not sure.
If your new functionality relates to the Flex Component LifeCycle in some manner, such as changing the display or the size you should be performing your changes in the lifecycle ethods; not in your set method. Something like this:
The invalidate methods will force the component lifecycle method to rerun during the next render event and you can use code like this in the relevant method:
您可以使用
get
和set
访问器方法。更多详细信息请参见此处。在你的情况下,它是这样的:
You can use
get
andset
accessor methods. More details is here.In you case it is something like:
我就是这样做的。创建一个调用您建议的方法的 setter 函数:
This is how I would do it. Create a setter function that calls the method you propose: