当 Flex 3 中的公共属性发生更改时,如何调用方法?

发布于 2024-11-06 23:52:54 字数 630 浏览 0 评论 0原文

如果我有一个 .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 技术交流群。

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

发布评论

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

评论(3

终止放荡 2024-11-13 23:52:54

由于还没有人说过这一点,我将提出第二种方法。

Flex 框架中的每个属性都会分派一个名为 *property*Changed 之类的属性。其中 property 是要更改的属性的名称。正如其他人提到的,所述属性是使用 get set 方法实现的。像这样的事情:

private var _myProperty:MyType;

[Bindable(myPropertyChanged)]
public function get myProperty():MyType
{
    return _myProperty;
}
public function set myProperty(value:MyType):void
{
    _myProperty = value;
   dispatchEvent(new Event('myPropertyChanged'));
}

可绑定元数据中指定的这些事件名称用于绑定目的。因此,您可以侦听此 myPropertyChanged 事件,而不是在集合内调用您的方法:

component.addEventListener('myPropertyChanged',onMyPropertyChanged)

在代码的其他地方:

protected function onMyPropertyChanged(event:Event):void{
 // do other processing
}

这对于您想要完成的任务来说可能有点过分了;或不。由于您没有详细说明您想要完成的任务,所以我不确定。

如果您的新功能以某种方式与 Flex 组件生命周期相关,例如更改显示或大小,您应该在生命周期方法中执行更改;不在你的 set 方法中。像这样的事情:

private var _myProperty:MyType;
private var _myPropertyChanged:Boolean = false
[Bindable('myPropertyChanged')]
public function get myProperty():MyType
{
    return _myProperty;
}
public function set myProperty(value:MyType):void
{
    _myProperty = value;
   _myPropertyChanged = true;
   invalidateProperties();
   invalidateDisplayList();
   invalidateSize()
   invalidateSkinState(); // spark comps only
   dispatchEvent(new Event('myPropertyChanged'));
}

invalidate 方法将强制组件生命周期方法在下一个渲染事件期间重新运行,您可以在相关方法中使用如下代码:

if(_myPropertyChanged == true){
  _myPropertyChanged = false;
  // do other processing
}

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:

private var _myProperty:MyType;

[Bindable(myPropertyChanged)]
public function get myProperty():MyType
{
    return _myProperty;
}
public function set myProperty(value:MyType):void
{
    _myProperty = value;
   dispatchEvent(new Event('myPropertyChanged'));
}

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:

component.addEventListener('myPropertyChanged',onMyPropertyChanged)

And elsewhere in the code:

protected function onMyPropertyChanged(event:Event):void{
 // do other processing
}

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:

private var _myProperty:MyType;
private var _myPropertyChanged:Boolean = false
[Bindable('myPropertyChanged')]
public function get myProperty():MyType
{
    return _myProperty;
}
public function set myProperty(value:MyType):void
{
    _myProperty = value;
   _myPropertyChanged = true;
   invalidateProperties();
   invalidateDisplayList();
   invalidateSize()
   invalidateSkinState(); // spark comps only
   dispatchEvent(new Event('myPropertyChanged'));
}

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:

if(_myPropertyChanged == true){
  _myPropertyChanged = false;
  // do other processing
}
饮湿 2024-11-13 23:52:54

您可以使用 getset 访问器方法。更多详细信息请参见此处

在你的情况下,它是这样的:

private var _myProperty:MyType;

public function set myProperty(value:MyType):void
{
    _myProperty = value;
    // he best way is to place myMethod body here
    myMethod(_myProperty);
}

[Bindable]
public function get myProperty():MyType
{
    return _myProperty;
}

You can use get and set accessor methods. More details is here.

In you case it is something like:

private var _myProperty:MyType;

public function set myProperty(value:MyType):void
{
    _myProperty = value;
    // he best way is to place myMethod body here
    myMethod(_myProperty);
}

[Bindable]
public function get myProperty():MyType
{
    return _myProperty;
}
靖瑶 2024-11-13 23:52:54

我就是这样做的。创建一个调用您建议的方法的 setter 函数:

var _mystatus:Number = 0;

function set mystatus(val:Number):void
{
_mystatus = val;
alertfunction();
}

function get mystatus():Number
{
return _mystatus;
}

This is how I would do it. Create a setter function that calls the method you propose:

var _mystatus:Number = 0;

function set mystatus(val:Number):void
{
_mystatus = val;
alertfunction();
}

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