Connecting flex and weborb?



我是flex的新手。我有一个问题:)

我有

[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。但当我再次点击时,一切都好了。

有人能帮我吗?谢谢?

模型。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. 在你的Algorithm类中定义如下:

    [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事件。DrawEvent。当您的客户端接收到Solve_Send调用的结果时,将会发送DATA_CHANGED。因此,ondatachchanged将绘制您的立方体或做任何您想做的事情:)

上面的方法是基本的,你必须知道事件是如何在flex中工作的,以及如何处理它。更多信息可在此处查看:

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

问候,西里尔

最新更新