简体   繁体   English

如何在Flex中动态更新mxml属性?

[英]How do I update an mxml property dynamically in Flex?

I'm using Flex 4.0, with the Halo theme, and a custom component that I have the source of. 我正在使用带有Halo主题的Flex 4.0和一个自定义组件。 I want to call the component with a parameter to hide or show some items. 我想用一个参数调用该组件以隐藏或显示一些项目。 The parameter is dynamic, meaning it is set in a method after the mxml has loaded. 该参数是动态的,这意味着它是在mxml加载后在方法中设置的。

Snippet: 片段:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute"
    xmlns:ns1="de.aggro.components.*"
    width="100%"
    height="100%"
    initialize="init();"
>

<mx:Script>
    <![CDATA[
        [Bindable]
        public var allowwindowmanipulation:Boolean = true;

        internal function init():void {
            allowwindowmanipulation = false;
        }
    ]]>
</mx:Script>
<mx:states>
    <mx:State name="st">
        ...
        <ns1:CollapsableTitleWindow width="956" height="395" x="294" y="484" id="wnd" title="title" allowClose="{allowwindowmanipulation}">
    </mx:State>
</mx:states>

</mx:Application>

CollapsableTitleWindow code (snippet): CollapsableTitleWindow代码(摘要):

public class CollapsableTitleWindow extends Panel implements ICTWindow {    
    public var allowClose:Boolean = true;

    function CollapsableTitleWindow(){
        super();
        addEventListener(FlexEvent.CREATION_COMPLETE, initializeHandler);
    }

    protected override function createChildren():void{
        super.createChildren();         

        //Create the Buttons
        closeButton = new Sprite();

        if(allowClose==true){
            titleBar.addChild(closeButton);
            drawCloseButton(true);
        }
    }
}

Now, when I run this code, the value given to the constructor of CollapsableTitleWindow is true for the allowClose variable. 现在,当我运行此代码,给的构造函数的值CollapsableTitleWindowtrueallowClose变量。 So when createChildren() is called, the button is drawn. 因此,当调用createChildren()时,将绘制按钮。

How can I change the behaviour? 我该如何改变行为? I don't mind if the button first gets drawn then later gets removed, if need be, but I don't know how I do this binding of parameter values. 我不介意是否先绘制按钮,然后再将其删除(如果需要),但是我不知道如何绑定参数值。

When I for instance change the title property to {allowClose} , the boolean value of false gets shown, even though probably a true is passed at first. 例如,当我将title属性更改为{allowClose} ,将显示false的布尔值,尽管可能最初会传递true

Note: edited to reflect feedback 注意:编辑以反映反馈

Since you're using Flex 4, you should probably do this with a skin and States within the skin. 由于您正在使用Flex 4,因此您可能应该使用皮肤和皮肤中的状态来执行此操作。 But if you want to do it through code, try something like this: 但是,如果要通过代码完成此操作,请尝试如下操作:

protected var _allowClose:Boolean=true;
protected var _allowCloseChanged::Boolean=true;
protected var _closeButton:Sprite = new Sprite();


public function get allowClose():Boolean {
    return _allowClose;
}

public function set allowClose(value:Boolean):void {
    if (value != _allowClose) {
       _allowClose=value;
       _allowCloseChanged=true;
       invalidateProperties();
    }
}

override protected function commitProperties():void {
   if (_allowCloseChanged) {
       if (_allowClosed) {
            titlebar.addChild(_closeButton);
            drawCloseButton(true);
       } else {
            titleBar.removeChild(_closeButton);
       }
       //you need to set this back false...sorry I left that out
       _allowCloseChanged=false;
   }
   super.commitProperties();
}

Note: in this case, you no longer need to override createChildren 注意:在这种情况下,您不再需要覆盖createChildren

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM