Flex - ResizeEvent.RESIZE 问题

发布于 2024-08-18 15:38:14 字数 156 浏览 7 评论 0原文

我的调整大小事件遇到了一个小问题...我自己创建了一个组件,并且该组件在每个调整大小事件中都运行一个复杂的方法。问题是:当我最大化或恢复窗口时,该事件在短时间内被多次调用,没有让我的方法有足够的时间在另一个方法开始之前完全运行...我想知道如果无论如何只能在每次调整大小运动结束时运行此方法。 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟燃烟灭 2024-08-25 15:38:14

不是对 ResizeEvent 做出反应,ResizeEvent 会在调整大小期间触发每一帧(即,如果您点击最大化并且组件需要 3 帧才能从 widh/ehight = 0 重绘到 width/height = MaxValue),则将触发调整大小事件三次,您可以“观察”宽度和高度的属性。

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

实际上,这将监视属性,类似于数据绑定,并在宽度或高度发生变化时执行 resizeHandler。这有一个小小的缺点,如果宽度和高度同时改变,则触发处理程序两次,但这可以通过执行该工作的第三个函数来解决,如果不安排调用该函数,则该函数将由处理程序调用。代码:

<?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>

我更喜欢更改观察程序方法,因为它在宽度和高度属性更改后触发,这与在宽度和高度属性正确更新之前触发的调整大小事件不同。

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.

吃→可爱长大的 2024-08-25 15:38:14

您需要将调整大小事件合并为单个事件。 David Coletta 在此演示文稿中简要讨论了事件合并,但我不记得他是否描述了他是如何做到的。

您可以通过多种方式来完成此操作,我将在下面的伪代码中布局其中一种:

resizeHandler(resizeEvent):
   if (resizeEventReceived)
       return
   resizeEventReceived = true
   Timer.startIn(30 /*ms*/).withHandler(doResize)

doResize(timerEvent):
    /* do actual resize processing */
    resizeEventReceived = false

You'll want to coalesce the resize events into a single event. David Coletta talked briefly about event coalescing in this presentation, but I don't remember if he described how he did it.

You could do this in a number of ways, one of which I'll layout in pseudo code below:

resizeHandler(resizeEvent):
   if (resizeEventReceived)
       return
   resizeEventReceived = true
   Timer.startIn(30 /*ms*/).withHandler(doResize)

doResize(timerEvent):
    /* do actual resize processing */
    resizeEventReceived = false
思念满溢 2024-08-25 15:38:14

在我看来,您正在事件处理程序中执行某些操作,导致触发新的 ResizeEvent.RESIZE 。由于 ActionScript 在单个线程中运行,因此在处理程序运行时没有独立的源可以调度该事件,除非您以某种方式触发它。

It sounds to me like you're doing something in your event handler that is causing a new ResizeEvent.RESIZE to be fired. Since ActionScript runs in a single thread, no independent source could possibly dispatch that event while your handler is running unless you somehow trigger it.

旧夏天 2024-08-25 15:38:14

我想指出的是,flex 具有完整的组件生命周期,这可能会帮助您:

尝试一下:

  1. 不要直接计算屏幕变化,而是在 updateDisplayList 方法中执行此操作。
  2. 当接收到 ResizeEvent 时,仅调用 invalidateDisplayList() 方法,该方法设置一个框架标志,该标志将触发 updateDisplayList() 的执行。

您可以在此处阅读有关 Flex3 组件生命周期的更多信息:
http://www.dlgsoftware.com/primers/Primer_on_Flex3_Component_Lifecycle.htm

I would like to point out that flex has a complete Component lifecycle which might help you:

Try this:

  1. do not directly calculate your screen changes, but do this in the updateDisplayList method.
  2. When receiving the ResizeEvent, only call the invalidateDisplayList() method, which sets a framework flag that will trigger execution of updateDisplayList()

You can read more about the Flex3 Component lifecycle here:
http://www.dlgsoftware.com/primers/Primer_on_Flex3_Component_Lifecycle.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文