WPF 强制重绘画布

发布于 2024-11-06 01:02:44 字数 339 浏览 1 评论 0原文

好的,在 Windows 窗体中,您可以使用 .refresh() 在元素上引发重绘事件。 WPF中有类似的解决方案吗?

对我正在做的事情的解释,我正在画布对象上绘制一个迷宫,并且希望观看迷宫的绘制过程(这样我就可以看到进度),而不是等待 28 分钟直到解决方案突然出现。我正在画布上用一系列 矩形 绘制块。刷新应该在矩形还是画布上?

这是最近的输出: https://i.sstatic.net/ZNAdQ.jpg

我想要一个 C# 解决方案如果可能的话。谢谢。

Okay, in windows forms you can use .refresh() to cause a redraw event on an element. Is there a similar solution in WPF?

An explanation of what I'm doing, I'm drawing a maze on a canvas object, and would like to watch as the maze is drawn (so I can see progress) instead of waiting 28 min for a solution to suddenly appear. I am drawing blocks on the canvas with a series of Rectangles. Should the refresh be on the rectangle or the canvas?

Here is a recent output:
https://i.sstatic.net/ZNAdQ.jpg

I'd like a solution in c# if that is possible. Thanks.

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

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

发布评论

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

评论(4

遮了一弯 2024-11-13 01:02:44

这就是您正在寻找的...

element.InvalidateVisual();

This is what you are looking for...

element.InvalidateVisual();
楠木可依 2024-11-13 01:02:44

这对我来说工作...

element.UpdateLayout();

this work for me...

element.UpdateLayout();
夏九 2024-11-13 01:02:44

您可能想要使用 Dispatcher 对象。我建议您看一下 Shawn Wildermuth 的文章:使用调度程序构建更多响应式应用程序(MSDN 杂志,2007 年 10 月)。

You probably want to use the Dispatcher object. I suggest you take a look a this Shawn Wildermuth's article: Build More Responsive Apps With The Dispatcher (MSDN magazine October 2007).

撩发小公举 2024-11-13 01:02:44

如果没有一个好的最小、完整且可验证的代码示例来清楚地显示您在做什么,就不可能确定要做什么这里最好的答案是。但是,从您的描述来看,迷宫生成算法似乎是在 UI 线程中执行,从而阻止 UI 自行更新。

就像 Winform 的情况一样,人们很容易调用 Refresh()Application.DoEvents() 之类的方法,这里真正的问题是您正在阻塞 UI 线程。解决这个问题的正确方法是,不要这样做

有很多替代方案,如果没有更详细的问题,就无法知道适合您的情况的最佳方法是什么。然而,两种最常用且最可能合适的技术是结合使用 BackgroundWorkerTask.Run() 以及 Progress方法。类。无论哪种情况,您的算法都会在不同的线程中运行,定期将更新传递给 UI 线程(例如每个矩形、每十个矩形等)。 UI 线程接收更新,将数据添加到视觉效果,然后返回等待下一次更新。

BackgroundWorkerProgress 都提供了自动将数据编组回 UI 线程的内置机制。唯一需要注意的是,无论您使用哪个类,都需要在 UI 线程上创建该类的实例。由于需要在开始执行异步工作之前设置它们,因此这通常不是问题;它是免费的。

如果你这样做,你将不需要任何黑客,例如到目前为止这里建议的三种不同的黑客(其中两个似乎不太适用于“我已经阻止了我的 UI 线程,现在怎么办?”无论如何)。

Without a good Minimal, Complete, and Verifiable code example to show clearly what you're doing, it's impossible to know for sure what the best answer here is. However, from your description it sounds as though the maze-generating algorithm is executing in the UI thread, blocking the UI from updating itself.

Just as in the case with Winforms, where people are tempting to call methods like Refresh() or Application.DoEvents(), the real problem here is that you're blocking the UI thread. The right way to fix that is, don't do that.

There are lots of alternatives and without a more detailed question there's no way to know what would be the best approach in your case. However, the two most commonly used and most likely to be appropriate techniques are to use BackgroundWorker, or Task.Run() in combination with the Progress<T> class. In either case, your algorithm runs in a different thread, passing updates to the UI thread periodically (e.g. every rectangle, every ten rectangles, whatever). The UI thread receives an update, adds the data to the visuals, and then goes back to waiting for the next update.

BackgroundWorker and Progress<T> both provide built-in mechanisms for automatically marshalling data back to the UI thread. The only caveat is that whichever of those classes you're using, the instance of that class needs to be created on the UI thread. Since they need to be set up before you start executing the asynchronous work, this is typically not a problem; it comes for free.

If you do it this way, you won't need any hacks such as the three different ones that have been suggested here so far (two of which don't seem likely to be applicable in the "I've blocked my UI thread, now what?" scenario anyway).

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