关于在 WPF 中刷新 UI 的困惑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15247594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Confusion about Refreshing the UI in WPF
提问by snowy hedgehog
I have heard that this refreshes the UI but why so? Dispatcherinvokes this empty action and thats it there is no call InvalidateMeasure()which would trigger the UI to re-measure and re-arrange and re-render inside the action. Where is here the measure and arrange process to update/refreshthe UI?
我听说这会刷新 UI,但为什么呢?Dispatcher调用这个空操作,那就是没有调用InvalidateMeasure()会触发 UI 在操作内重新测量、重新排列和重新渲染。update/refreshUI的度量和安排过程在哪里?
private static Action EmptyDelegate = delegate() { };
public static void Refresh(UIElement uiElement)
{
uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
}
Any help?
有什么帮助吗?
EDITED:I want to know the details why is UI rendered. Answers like well that triggers ui updateare not helping me any further guys.
EDITED:我想知道为什么渲染 UI 的细节。像这样well that triggers ui update的答案对我没有帮助。
采纳答案by Jakob Christensen
Microsoft recommends a slighty different way of doing what your code does using PushFrame.
Microsoft 建议使用稍有不同的方式来执行您的代码使用PushFrame所做的事情。
The code has nothing to do with rendering. What it does is similar to the DoEventsmethod known from VB6 and WinForms. DoEventshalts the program while pumping the Windows message queue and handling the messages in the queue. This includes any rendering messages such as WM_PAINTand WM_NCPAINT.
代码与渲染无关。它的作用类似于DoEventsVB6 和 WinForms 中已知的方法。 DoEvents在泵送 Windows 消息队列和处理队列中的消息时暂停程序。这包括任何呈现消息,例如WM_PAINT和WM_NCPAINT。
The code instructs the Dispatcher to halt the current thread and pump its queue and handle all messages that have a priority of DispatcherPriority.Render or higher. If there are any pending rendering messages in the queue, for example if InvalidateRecthas been called somewhere by the framework, these messages will be handled and the UI will be re-rendered.
该代码指示 Dispatcher 停止当前线程并抽取其队列并处理具有 DispatcherPriority.Render 或更高优先级的所有消息。如果队列中有任何待处理的渲染消息,例如,如果InvalidateRect框架在某处调用了这些消息,则将处理这些消息并重新渲染 UI。
DoEventshas always been considered a code smell because it bypasses the message queue. If you want a responsive user interface you should use worker threads instead.
DoEvents一直被认为是代码异味,因为它绕过了消息队列。如果您想要响应式用户界面,则应改用工作线程。
回答by davisoa
This can be used to force WPF to update the UI because of the priority of the invocation. Since this invoke uses the Dispatcher synchronously, any tasks that are already queued with an equal or higher priority than DispatcherPriority.Renderwill be run before running the delegate you provided.
由于调用的优先级,这可用于强制 WPF 更新 UI。由于此调用同步使用 Dispatcher,DispatcherPriority.Render因此在运行您提供的委托之前,任何已以等于或高于优先级的优先级排队的任务都将运行。
Here is the list of potential values of DispatcherPriority, and it explains the order tasks are run by the dispatcher.
这是DispatcherPriority的潜在值列表,它解释了由调度程序运行的订单任务。

