开发者

Flex ChangeWatcher doesn't work

I'm trying to implement a binding between some custom-built models and just beginning to dabble with the whole mx.binding.* collection. I tried this simple, stripped down example, but can't get the binding working correctly. Can somebody tell me where I'm going wrong?

// Model
package  
{
 import flash.events.EventDispatcher;

 public class Model extends EventDispatcher
 {
  private var m_count:uint = 0;

  [Bindable]
  public function get Count():uint
  {
   return this.m_count;
  }

  public function set Count(c:uint):void
  {
   this.m_count = c;
  }
 }
}

And this is what the application MXML looks like

// MXML
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:core="*" creationComplete="this.init();">
 <mx:Script>
  <![CDATA[
  import flash.events.Event;
  import flash.utils.describeType;
  import mx.binding.utils.ChangeWatcher;

  [Bindable]
  public var model:Model;

  public function init():void
  {
   var _this:Object = this;

   this.addEventListener(Event.ENTER_FRAME, function(e:Event):void {
    _this.model.Count++;
   });


   this.model = new Model();

   trace(ChangeWatcher.canWatch(this.model, "Count")); // This always returns false for some reason
   trace(describeType(this.model));
  }

  public function UpdateText(s:String):void
  {
   trace(s);
  }
  ]]>
 </mx:Script>
 <mx:Text text="{this.model.Count}" creationComplete="trace(this);" />
</mx:WindowedApplication>

Update: I tried an even more bare-bones version as shown below.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="this.m_init();">
    <mx:Script>
        <![CDATA[
        import mx.binding.utils.ChangeWatcher;

        [Bindable] public var m:Object = new Object();

        public function m_init():void
        {
            trace(ChangeWatcher.canWatch(this, "m"));
        }
        ]]>
    </mx:Script>
    <mx:Text text="{this.m}" />
</mx:Application>
开发者_开发问答

Still. Doesn't. Work. ChangeWatcher.canWatch still returns false, although the textfield does display [object Object].


  public function init():void
  {
    this.addEventListener(Event.ENTER_FRAME, increment);
    this.model = new Model();
  }
  public function increment(e:Event):void 
  {
    if(this.model)
      this.model.Count++;
  }

<mx:Text text="{model.Count}" creationComplete="trace(this);" /--> 


i can give u one example of changeWatcher, that may be some help, right now i m working with this, it's helpful,

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
<mx:Style source="../assets/css/scaleCSS.css"/>
<mx:Script>
 <![CDATA[
  import mx.binding.utils.ChangeWatcher;
  import mx.controls.Text;
  private var text:Text;

  [Bindable]private var ti1mc:int=50;

  private function init():void
  {
   ChangeWatcher.watch(ti1,'text',updateTextti);
   ChangeWatcher.watch(ti2,'text',updateTextti);
   ChangeWatcher.watch(ti3,'text',updateTextti);
   ChangeWatcher.watch(ti4,'text',updateTextti);
   ChangeWatcher.watch(ti5,'text',updateTextti);
  }

  private function updateTextti(event:Event):void
  {
   var str:String;
   str=event.currentTarget.id.slice(2,3);
//   trace("str : "+str);
//   trace(Canvas(textbox.getChildAt(int(TextInput(event.currentTarget).id.slice(2,3))-1)).id);
   Text(Canvas(textbox.getChildAt(int(TextInput(event.currentTarget).id.slice(2,3))-1)).getChildAt(0)).text=event.currentTarget.text;
//   trace(Text(Canvas(textbox.getChildAt(int(TextInput(event.currentTarget).id.slice(2,3))-1)).getChildAt(0)).id);
//   trace(event.currentTarget.id.slice(2,3));
    if(Canvas(textbox.getChildAt(int(TextInput(event.currentTarget).id.slice(2,3))-1)).width>=textbox.width)
    event.currentTarget.maxChars=event.currentTarget.length;
   else
    event.currentTarget.maxChars=50;
  } 
 ]]>
</mx:Script>
 <mx:Canvas width="80%" height="80%" horizontalCenter="0" verticalCenter="0" borderStyle="solid">
  <mx:VBox id="textbox" height="300" width="200" borderStyle="solid" top="50" left="100" horizontalScrollPolicy="off" verticalScrollPolicy="off">
   <mx:Canvas id="textcan1" borderStyle="solid" borderColor="#FF0000">
    <mx:Text id="text1" fontFamily="Arbeka" styleName="text" rotation="20"/>
   </mx:Canvas>
   <mx:Canvas id="textcan2" borderStyle="solid" borderColor="#FFFF00">
    <mx:Text id="text2" fontFamily="Arbeka" styleName="text" rotation="20"/>
   </mx:Canvas>
   <mx:Canvas id="textcan3" borderStyle="solid" borderColor="#00FF00">
    <mx:Text id="text3" fontFamily="Arbeka" styleName="text" rotation="20"/>
   </mx:Canvas>
   <mx:Canvas id="textcan4" borderStyle="solid" borderColor="#0000FF">
    <mx:Text id="text4" fontFamily="Arbeka" styleName="text" rotation="20"/>
   </mx:Canvas>
   <mx:Canvas id="textcan5" borderStyle="solid" borderColor="#00FFFF">
    <mx:Text id="text5" fontFamily="Arbeka" styleName="text" rotation="20"/>
   </mx:Canvas>
  </mx:VBox>
  <mx:VBox id="textinputbox" height="300" width="200" top="50" right="100" borderStyle="solid" verticalGap="0">
   <mx:TextInput id="ti1" width="100%" maxChars="50"/>
   <mx:TextInput id="ti2" width="100%" maxChars="50"/>
   <mx:TextInput id="ti3" width="100%" maxChars="50"/>
   <mx:TextInput id="ti4" width="100%" maxChars="50"/>
   <mx:TextInput id="ti5" width="100%" maxChars="50"/>
  </mx:VBox>  
 </mx:Canvas>
</mx:Application>

here ti1(textinput) text property is being watched, if there is a change in to the property, then the handler function(updatetextti) will be called

hope this helps


I removed all files inside the obj folder under the project and the problem seems to have gone away. It might have been that FlashDevelop was using older compiled binaries out of this folder, which made the output go out of sync with the source code.

Evidence to support this lies in two things. Changing the SDK did not affect the output on my own computer, which ruled out any SDK-specific issues. So I created a new project in another IDE on the same computer and copy-pasted the code into that one. This time it worked. That's when I thought it might be the cache and went in and deleted it.

In hindsight, I should have renamed it, and later tried to compile with the old cache in place again. That would have been conclusive.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜