2011-11-03 38 views
1

即時通訊軟件中的新手。林有一個問題:)連接flex和weborb?

[Bindable] 
      private var model:AlgorithmModel = new AlgorithmModel(); 
      private var serviceProxy:Algorithm = new Algorithm(model); 

在MXML

    private function Show():void 
     { 

      // now model.Solve_SendResult = null 
      while(i<model.Solve_SendResult.length) // 
      { 
       Draw(); //draw cube 
      } 
     } 
        private function Solve_Click():void 
     { 
      //request is a array 
      Request[0] = 2; 
      Request[1] = 2; 
      Request[2] = 3; 
      serviceProxy.Solve_Send(request); 

      Show(); 

     } 
<s:Button x="386" y="477" label="Solve" click="Solve_Click();"/> 

當我打電話serviceProxy.Solve_Send(request);與請求數組,我想在我的代碼使用model.Solve_SendResult FLEX吸引許多立方體使用papervison3d但在第一次我收到model.Solve_SendResult = null。但是當我再次點擊時,一切正常。

有人幫我嗎?謝謝?

回答

0

model.Solve_SendResult對象包含執行的serviceProxy.Solve_Send(request)方法的結果。 Solve_Send將被異步執行,因此,在啓動show方法的時刻,Solve_SendResult對象可能仍爲空。

作爲一個解決方案,你可以使用以下命令:

  1. 創建自定義事件

    package foo 
    { 
    import flash.events.Event; 
    
    public class DrawEvent extends Event 
    { 
    public static const DATA_CHANGED:String = "dataChanged"; 
    
    public function DrawEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) 
    { 
        super(type, bubbles, cancelable); 
    } 
    } 
    } 
    
  2. 在你的算法類中定義如下:

    [Event(name=DrawEvent.DATA_CHANGED, type="foo.DrawEvent")] 
    public class Algorithm extends EventDispatcher{ 
    //your code 
    
  3. 在Algorithm類的Solve_SendHandler方法添加以下

    public virtual function Solve_SendHandler(event:ResultEvent):void 
    { 
    dispatchEvent(new DrawEvent(DrawEvent.DATA_CHANGED)); 
    //your code 
    } 
    
  4. 在MXML類中創建的onLoad方法和添加事件偵聽器的算法類的實例,就像下面這樣:

    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="onLoad()"> 
    
    public function onLoad():void 
    { 
        serviceProxy.addEventListener(DrawEvent.DATA_CHANGED, onDataChanged); 
    } 
    
    private function onDataChanged(event:DrawEvent):void{ 
    while(i<model.Solve_SendResult.length) // 
        { 
         Draw(); //draw cube 
        } 
    } 
    
  5. 使在Solve_Click以下更改()方法:

    private function Solve_Click():void 
    { 
        //request is a array 
        Request[0] = 2; 
        Request[1] = 2; 
        Request[2] = 3; 
        serviceProxy.Solve_Send(request); 
    } 
    

這就是它!所以,基本上上面的代碼執行以下操作:向服務添加了一個偵聽器(算法類),並且偵聽器正在偵聽DrawEvent.DATA_CHANGED事件。當您的客戶端收到Solve_Send調用的結果時,將調度DrawEvent.DATA_CHANGED。因此,onDataChanged將繪製您的立方體或做任何你想要的:)

上面的方法是基本的,你必須知道事件如何在flex中工作以及如何處理它。更多信息,請訪問:

http://livedocs.adobe.com/flex/3/html/help.html?content=createevents_3.html http://livedocs.adobe.com/flex/3/html/help.html?content=events_07.html

問候, 西里爾