WPF - LayoutUpdated 事件反复触发 [英] WPF - LayoutUpdated event firing repeatedly

查看:23
本文介绍了WPF - LayoutUpdated 事件反复触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的 WPF 应用程序添加一些动画.

I've been adding a bit of animation to my WPF application.

感谢 Dan Crevier 的独特解决方案,用于为面板的孩子设置动画 结合 很棒的 WPF Penner 动画 事实证明,让我的一个控件看起来很棒并让它的孩子通过一些漂亮的动画移动是相当简单的.

Thanks to Dan Crevier's unique solution to animating the children of a panel combined with the awesome WPF Penner animations it turned out to be fairly straightforward to make one of my controls look great and have its children move about with some nice animation.

不幸的是,这一切都会带来性能开销.我很高兴在添加/删除项目或调整控件大小时性能受到影响,但似乎这种性能影响在整个应用程序的整个生命周期中始终如一,即使项目完全静态也是如此.

Unfortunately this all comes with a performance overhead. I'm happy to have the performance hit when items are added/removed or the control is resized, but it seems that this perf hit occurs consistently throughout the application's lifetime, even when items are completely static.

PanelLayoutAnimator 类使用附加属性来挂钩 UIElement.LayoutUpdated 事件.当这个事件触发时,渲染变换会被动画化,让孩子们滑到他们的新位置.

The PanelLayoutAnimator class uses an attached property to hook the UIElement.LayoutUpdated event. When this event fires, render transforms are animated to cause the children to glide to their new positions.

不幸的是,LayoutUpdated 事件似乎每秒触发一次,即使应用程序中没有发生任何事情(至少我认为我的代码没有做任何事情 - 应用程序没有有焦点并且鼠标是稳定的.)由于事件的原因对事件处理程序来说不是很明显,因此必须重新评估控件的所有子级.空闲时大约每秒调用一次此事件.实际使用应用时频率会增加.

Unfortunately it seems that the LayoutUpdated event fires every second or so, even when nothing is happening in the application (at least I don't think my code's doing anything -- the app doesn't have focus and the mouse is steady.) As the reason for the event is not immediately apparent to the event handler, all children of the control have to be reevaluated. This event is being called about once a second when idle. The frequency increases when actually using the app.

所以我的问题是,我怎样才能提高这里的性能?任何有帮助的答案将不胜感激,但我目前被困在这些子问题上:

So my question is, how can I improve the performance here? Any answer that assists would be appreciated, but I'm currently stuck on these sub-questions:

  1. 是什么导致 LayoutUpdated 事件如此频繁地触发?这是否应该发生,如果没有,我如何才能找出它为什么会触发并减少它?

  1. What causes the LayoutUpdated event to fire so frequently? Is this supposed to happen, and if not, how can I find out why it's firing and curtail it?

在处理程序中是否有更方便的方法来了解是否发生了可能移动了孩子的事情?如果是这样,我可以尽早退出并避免循环每个孩子的开销.

Is there a more convenient way within the handler to know whether something has happened that might have moved children? If so, I could bail out early and avoid the overhead of looping each child.

现在我将通过在面板中超过 N 个孩子时禁用动画来解决这个问题.

For now I will work around this issue by disabling animation when there are more than N children in the panel.

推荐答案

如果您从附加到 LayoutUpdated 事件的 EventHandler 更新 UI,这将也触发那个事件!

If you're updating the UI from the EventHandler you attached to the LayoutUpdated event, this will also trigger that event!

例如:

void my_LayoutUpdatedEvent(object sender, EventArgs e)
{
    textBlock1.Text = "changed";
    Console.Out.WriteLine("text changed!");
}
    

将进入无限更新循环.

也许你需要做这样的事情:

Perhaps you need to do something like this:

void my_BetterLayoutUpdatedEvent(object sender, EventArgs e)
{
    if (!hasChanged)
        textBlock1.Text = "changed";
    hasChanged = true;
    Console.Out.WriteLine("text changed!");
}

这篇关于WPF - LayoutUpdated 事件反复触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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