尝试使用BindingUtils在Flash AS3.0 [英] Trying to use BindingUtils in Flash AS3.0

查看:169
本文介绍了尝试使用BindingUtils在Flash AS3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能够使AS3.0(闪存)这个code与工作的Flex SDK 4.0包括在内。

I am not able to make this code work in AS3.0 ( Flash ) with Flex SDK 4.0 included.

 import mx.binding.utils.*;


 [Bindable]
 var myValue:int = 0;
 var cw:ChangeWatcher = BindingUtils.bindSetter(myValueChanged, this, "myValue");
 addEventListener( Event.ENTER_FRAME , ef);

 function ef(e:Event):void
 {
    trace("hello",getTimer());
    myValue = getTimer();
 }

 function myValueChanged(o:Object):void
 {
    trace("myValue: " + myValue.toString());
 }

我得到的输出是:

The output i get is :

myValue: 0
hello 157
hello 168
hello 171
hello 177
....
....

等。

但我相信正确的输出应该是:

But I expect the correct output should be :

myValue: 0
hello 157
myValue: 157
hello 168
myValue: 168
hello 171
myValue: 171
hello 177
myValue: 177
....
....

感谢。

推荐答案

我不完全知道如何闪光灯临编译器将这些东西(我一直使用免费的mxmlc编译从Flex SDK)。

I'm not exactly sure how the Flash Pro compiler treats this stuff (I always use the free mxmlc compiler from the Flex SDK).

为你工作的呢?

package
{
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.utils.getTimer;
  import mx.binding.utils.BindingUtils;

  public class BindingExample extends Sprite {

    private var model:Model;

    public function BindingExample()
    {
      model = new Model();
      BindingUtils.bindProperty(this, 'spy', model, ['value']);
      addEventListener( Event.ENTER_FRAME , onEnterFrame);
    }

    public function onEnterFrame(e:Event):void
    {
      model.value = getTimer();
    }

    public function set spy(value:int):void
    {
      trace('Bound property set to: ' + value);
    }
  }
}

class Model
{
  [Bindable]
  public var value:int;
}

如果没有,尝试一下本作的模型定义:

If not, try this for the Model definition:

import flash.events.Event;
import flash.events.EventDispatcher;

class Model extends EventDispatcher
{
  private var _value:int;

  [Bindable("valueChange")]
  public function get value():int
  {
    return _value;
  }

  public function set value(value:int):void
  {
    if (_value != value)
    {
      trace('Model property set to: ' + value);
      _value = value;
      dispatchEvent(new Event("valueChange"));
    }
  }
}

如果还是不行,请尝试以下的型号:

If that doesn't work, try this for the Model:

import flash.events.EventDispatcher;
import mx.events.PropertyChangeEvent;
import mx.events.PropertyChangeEventKind;

class Model extends EventDispatcher
{
  private var _value:int;

  [Bindable("propertyChange")]
  public function get value():int
  {
    return _value;
  }

  public function set value(value:int):void
  {
    if (_value != value)
    {
      trace('Model property set to: ' + value);
      var oldValue:int = _value;
      _value = value;
      dispatchEvent(new PropertyChangeEvent(
        PropertyChangeEvent.PROPERTY_CHANGE, false, false,
        PropertyChangeEventKind.UPDATE, "value", oldValue, value, this));
    }
  }
}

或者,也许有一个ObjectProxy:

Or, perhaps with an ObjectProxy:

package
{
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.utils.getTimer;
  import mx.binding.utils.BindingUtils;
  import mx.utils.ObjectProxy;

  public class BindingExample extends Sprite {

    private var model:ObjectProxy;

    public function BindingExample()
    {
      model = new ObjectProxy({value: 0});
      BindingUtils.bindProperty(this, 'spy', model, ['value']);
      addEventListener( Event.ENTER_FRAME , onEnterFrame);
    }

    public function onEnterFrame(e:Event):void
    {
      model.value = getTimer();
    }

    public function set spy(value:int):void
    {
      trace('Bound property set to: ' + value);
    }
  }
}

使用mxmlc的编译时,以上所有的做工精细。我想避免的一个ObjectProxy因为它至少类型安全。

All of the above work fine when compiling with mxmlc. I'd avoid the ObjectProxy as it's the least typesafe.

这篇关于尝试使用BindingUtils在Flash AS3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆