2013-02-27 16 views
0

我有一個mxml應用程序,如下所示。如何從actionscript文件更改標籤的文本?從動作腳本訪問mxml中的元素

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="initApp()"> 
    <fx:Script> 
     <![CDATA[ 
      public function initApp(){ 
       var p = new my_player("a"); 
      } 
     ]]> 
    </fx:Script> 
    <s:Label x="700" y="409" text="Label" id="lble" width="131" height="41"/> 
</s:Application> 

my_player.as代碼從主應用程序

package 
{ 
    import spark.components.Label; 
    public class my_player 
    { 
     public var lble:Label; 
     public function my_player(a:String) 
     { 
      lble.text="hello"; 
     } 
    } 
} 
+0

該代碼打破了OOP封裝的基本原理。或者我不明白這個問題。 – 2013-02-27 12:24:58

+0

@IlyaZ它如何打破封裝原則? – vikingmaster 2013-02-27 12:40:00

+0

你有兩個應用程序實例,這沒有多大意義。如果你將ActionScript從MXML中分離出來,你應該對Flex 4(Spark)皮膚結構進行一些研究。 – RIAstar 2013-02-27 12:45:57

回答

0

進樣標籤與構造函數convey_player(另一種方式 - 綁定)

public function initApp(){ 
    var p = new my_player("a", lble); 
} 

public function my_player (a:String, targetLabel:Label) 
{ 
    lble = targetLabel; 
    lble.text="hello"; 
} 
0

你不需要第二班:(

<?xml version="1.0" encoding="utf-8"?> 
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    creationComplete="initApp()"> 
    <fx:Script> 
     <![CDATA[ 
     public function initApp(){ 

      // access your components by id 
      lble.text = "My new text for the label" 
     } 
     ]]> 
    </fx:Script> 
    <s:Label x="700" y="409" text="Label" id="lble" width="131" height="41"/> 
</s:Application> 
+0

我有4種類型的流媒體,每種都有獨立的類,因爲每種類都有很多代碼,所有代碼都在一個mxml文件中,對我來說是不可能的。 – 2013-02-28 16:33:22

+0

我擔心你正在使用錯誤的方法......在你的實現中我看不到OOP。請考慮給出一個更好的例子或更好的解釋你想要達到的目標,而不是如何解決你有的特定問題 – 2013-03-01 07:27:04