如何在 WPF 中持续更新 Canvas 上的形状?
我的 WPF 应用程序中有一个 Canvas 控件,我在不同的线程中创建大量形状并将它们添加到画布中(使用 Dispatcher),但由于我重新创建子级(它们是动态的,从其他数据生成,并且它们的数量在运行时发生变化),我调用:
canvas.Children.Clear();
但这样做会使画布“闪烁”,因此在画布被清理和填充时会产生闪烁的外观。是否可以使其看起来“连续”,以便干净的画布步骤不可见?
我不确定是否需要使用上述调用或执行其他操作来避免它。
I have a Canvas
control in my WPF app, and I am creating lots of shapes in a different thread and adding them to the canvas (using Dispatcher
), but since I am recreating the childrens (they are dynamic and generated from other data, and the number of them change at runtime), I call:
canvas.Children.Clear();
but doing so makes the "flashes" the canvas, so gives a flickering look as the canvas is getting cleaned and populated. Is it possible to make this appear "continuous" so the clean canvas step is not visible?
I am not sure if I need to use the above call or do something else to avoid it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Canvas 允许您管理其子级的位置,但它仍然负责绘图操作。如果你清除它的孩子,你实际上是在要求它仅用它的背景重新绘制自己。
您可以修改 Canvas 的子项,因此,如果您确定它仍然存在,我建议修改子项的属性(而不是删除它并重新添加它)。否则删除它并创建新的并添加它们。
A Canvas lets you manage the positioning of its children, but it is still responsible for the drawing operations. If you clear its children, you are actually requesting it to redraw itself with just its background.
You are allowed to modify a child of Canvas, so I suggest modifying the properties of a child (instead of removing it and re-adding it) if you determine that it should still exist. Otherwise remove it and create new ones and add them.
与其一次性清除所有 Canvas 子项,为什么不将它们一一删除并一一重新创建它们,例如,您可以为每个形状添加一个计时器,然后计时器在每次勾选后从 Canvas 子项中删除该形状,这样闪烁效果就会消失。
Instead of clearing all the Canvas children all at once, why not you remove them one by one and recreate them one by one, for example you could add a timer with each shape, then the timer removes that shape after each tick from the Canvas children, this way the flickering effect will disapear.