Flex - ResizeEvent.RESIZE的问题 [英] Flex - Problem with ResizeEvent.RESIZE

查看:117
本文介绍了Flex - ResizeEvent.RESIZE的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的resize事件有一点问题我自己创建了一个组件,这个组件在每个resize事件中运行一个复杂的方法。这是一个问题:当我最大化或恢复我的窗口时,这个事件在很短的时间内被叫好几次,没有足够的时间让我的方法在另一个开始之前完全运行...我想知道如果在每次调整大小运动结束时都只运行这种方法。
谢谢

解决方案

而不是对ResizeEvent做出反应,这将在调整大小期间触发每一帧(即如果你打最大化,组件重新从widh / ehight = 0重新绘制到width / height = MaxValue需要3帧),那么resize事件将触发三次,您可以观察宽度和高度的属性。

  var widthWatch:ChangeWatcher = ChangeWatcher.watch(this,'widht',resizeHandler)
var heightWatch:ChangeWatcher = ChangeWatcher.watch ,'height'resizeHandler)

实际上,这将会观察属性,类似于数据绑定,并在宽度或高度变化时执行resizeHandler。如果宽度和高度同时改变,这样会有一些轻微的缺点:如果宽度和高度同时变化,则可以使用第三个函数来解决这个问题,而这个函数可以在处理程序调用的时候被调用。代码:

 <?xml version =1.0encoding =utf-8?> 
< mx:Application xmlns:mx =http://www.adobe.com/2006/mxmllayout =absolutecreationComplete =onCreationComplete(event)>
< mx:Script>
<![CDATA [
import mx.binding.utils.ChangeWatcher;

private var widthWatch:ChangeWatcher;
private var heightWatch:ChangeWatcher;
private var resizeExecuting:Boolean = false;

private function onCreationComplete(event:Event):void
{
widthWatch = ChangeWatcher.watch(this,'width',onSizeChange);
heightWatch = ChangeWatcher.watch(this,'height',onSizeChange);
}

私有函数onSizeChange(event:Event):void
{
if(!resizeExecuting)
callLater(handleResize);
resizeExecuting = true;
}

私有函数handleResize():void
{
//在这里做昂贵的工作
resizeExecuting = false;
}

私有函数stopWatching()
{
//调用这个来停止监视属性并阻止handleResize方法执行
widthWatch.unwatch ();
heightWatch.unwatch();
}

]]>
< / mx:脚本>
< / mx:Application>

我喜欢更改观察器方法,因为在width和height属性更改后触发,与resize不同在宽度和高度属性相关更新之前触发的事件。


I'm having a little problem with my resize event... I created a component myself, and this component, at each resize event, runs a complex method. Here is the problem: When I maximize or restore my window, this event is called several times in a real short period of time, not letting enough time to my method to run completely before another one starts... I'd like to know if there is anyway of only run this method in the end of each resize movement. Thanks

解决方案

Instead of reacting to a ResizeEvent, which will fire every frame during a resize (i.e. if you hit maximize and it takes 3 frames for the component to redraw from widh/ehight = 0 to width/height = MaxValue) then the resize event will fire three times, you could 'watch' the properties of width and height.

var widthWatch:ChangeWatcher = ChangeWatcher.watch(this, 'widht', resizeHandler)
var heightWatch:ChangeWatcher = ChangeWatcher.watch(this, 'height' resizeHandler)

this will, in effect, watch the properties, similar to data binding, and execute your resizeHandler whenever the width or height change. This has a slight drawback as to firing your handler twice if width and height change at the same time, but that can be solved with a third function that does the work, which gets invoked by your handlers if it isn't schedule to be invoked. The code:

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

  private var widthWatch:ChangeWatcher;
  private var heightWatch:ChangeWatcher;
  private var resizeExecuting:Boolean = false;

  private function onCreationComplete(event:Event):void
  {
   widthWatch = ChangeWatcher.watch(this,'width',onSizeChange);
   heightWatch = ChangeWatcher.watch(this,'height',onSizeChange);
  }

  private function onSizeChange(event:Event):void
  {
   if(!resizeExecuting)
    callLater(handleResize);
   resizeExecuting = true;
  }

  private function handleResize():void
  {
   //do expensive work here
   resizeExecuting = false;
  }

  private function stopWatching()
  {
   //invoke this to stop watching the properties and prevent the handleResize method from executing
   widthWatch.unwatch();
   heightWatch.unwatch();
  }

 ]]>
</mx:Script>
</mx:Application>

I prefer the change watcher method because it fires after the width and height properties have changed, unlike a resize event wich fires before the width and height properties are correclty updated.

这篇关于Flex - ResizeEvent.RESIZE的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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