According to the Flex docs,只要屬性是可綁定的,你可以簡單地做到這一點(我已經包含了兩個額外的按鈕來演示):
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
[Bindable]
private var currentUser:String = "Bill";
private function isUserAllowed(user:String):Boolean
{
if (user == "Bill")
{
return true;
}
return false;
}
]]>
</mx:Script>
<mx:VBox>
<mx:Button label="My Button" enabled="{isUserAllowed(currentUser)}" />
<mx:HBox>
<mx:Button label="Try Tom" click="{currentUser = 'Tom'}" />
<mx:Button label="Try Bill" click="{currentUser = 'Bill'}" />
</mx:HBox>
</mx:VBox>
</mx:Application>
沒有currentUser標記爲[綁定],雖然,它贏得沒有工作。另一種方法,如果你想更直接地綁定到函數(這也表示在文檔中),可以讓函數響應當前用戶發生更改時發送的事件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
<mx:Script>
<![CDATA[
private var _currentUser:String = "Bill";
public function set currentUser(value:String):void
{
if (_currentUser != value)
{
_currentUser = value;
dispatchEvent(new Event("userChanged"));
}
}
[Bindable(event="userChanged")]
private function isUserEnabled():Boolean
{
if (_currentUser == "Bill")
{
return true;
}
return false;
}
]]>
</mx:Script>
<mx:VBox>
<mx:Button label="My Button" enabled="{isUserEnabled()}" />
<mx:HBox>
<mx:Button label="Try Tom" click="{currentUser = 'Tom'}" />
<mx:Button label="Try Bill" click="{currentUser = 'Bill'}" />
</mx:HBox>
</mx:VBox>
</mx:Application>
所以有幾種方法。國際海事組織,第二個看起來似乎更合適,但第一個沒有錯。祝你好運!
但是,在我的參數傳遞給函數isUserAllowed?我想在試圖設置enabled屬性時動態地調用該函數。 – SkunkSpinner 2009-01-15 01:50:34
試試這個,我更新了它。 – ForYourOwnGood 2009-01-15 01:54:40
該變量將成爲全局變量,不需要將其傳遞給該函數。 – ForYourOwnGood 2009-01-15 19:39:47