Apache flex 如何在Flex中动态更新mxml属性?
我使用的是Flex4.0,带有Halo主题,还有一个自定义组件,我有它的源代码。我想用一个参数调用组件来隐藏或显示一些项目。该参数是动态的,这意味着它是在mxml加载后在方法中设置的 片段:Apache flex 如何在Flex中动态更新mxml属性?,apache-flex,flex4,halo,Apache Flex,Flex4,Halo,我使用的是Flex4.0,带有Halo主题,还有一个自定义组件,我有它的源代码。我想用一个参数调用组件来隐藏或显示一些项目。该参数是动态的,这意味着它是在mxml加载后在方法中设置的 片段: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="de.aggro.components.*" width="100%" height="100%
<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>
现在,当我运行这段代码时,对于allowClose
变量,给CollapsableTitleWindow
的构造函数的值是true
。因此,当调用createChildren()
时,将绘制按钮
我怎样才能改变这种行为?我不介意按钮是否先被绘制,然后再被删除(如果需要的话),但我不知道如何绑定参数值
例如,当我将
title
属性更改为{allowClose}
时,会显示false
的布尔值,即使最初可能传递了true
。注意:编辑以反映反馈
因为您使用的是Flex4,所以您可能应该对皮肤和皮肤内的状态执行此操作。但如果您想通过代码来实现,请尝试以下方法:
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();
}
注意:在这种情况下,您不再需要重写createChildren我正在研究您的解决方案,正如我们所说的,它似乎被正确调用,但我的其余代码(我没有包含在代码段中)应该发生在“drawCloseButton”代码附近,目前正在失败,因此我正在首先修复它。.Joy,它工作了!(我不得不修改我自己的代码。)但是有两件事:commitProperties应该是“protectedoverride”而不是“public”,而“if(_allowCloseChanged){”应该是一个“_allowCloseChanged=false”;谢谢!抱歉,早上写得太早了:)。你不得不将它改为(!_allowCloseChanged)的原因是因为我忘了在commitProperties中将其设置回false。您希望在_allowClose发生更改时(因此使用该标志)运行该代码,而不是每次commitProperties运行时(通常是这样)。
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();
}