2011-11-03 58 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

问候, 西里尔