关于在WPF中刷新UI的困惑 [英] Confusion about Refreshing the UI in WPF

查看:368
本文介绍了关于在WPF中刷新UI的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说这会刷新UI,但为什么会这样呢? Dispatcher 调用此空操作,并且没有调用 InvalidateMeasure(),这将触发UI重新测量并在动作内部重新安排和重新渲染。

I have heard that this refreshes the UI but why so? Dispatcher invokes 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/refresh the UI?

private static Action EmptyDelegate = delegate() { };

public static void Refresh(UIElement uiElement)
{
   uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
}

有任何帮助吗?

已编辑:我想知道为什么渲染UI的详细信息。像 well这样触发ui更新的答案并不能帮助我。

EDITED: I want to know the details why is UI rendered. Answers like well that triggers ui update are not helping me any further guys.

推荐答案

Microsoft建议使用与 PushFrame

Microsoft recommends a slighty different way of doing what your code does using PushFrame.

代码与渲染无关。它的作用类似于 <$ c $ VB6和WinForms中已知的c> DoEvents 方法。 DoEvents 在抽入Windows消息队列并处理队列中的消息时会暂停程序。这包括任何呈现消息,例如 WM_PAINT WM_NCPAINT

The code has nothing to do with rendering. What it does is similar to the DoEvents method known from VB6 and WinForms. DoEvents halts the program while pumping the Windows message queue and handling the messages in the queue. This includes any rendering messages such as WM_PAINT and WM_NCPAINT.

该代码指示分派器暂停当前线程并抽出其队列并处理所有优先级为DispatcherPriority.Render或更高的消息。如果队列中有任何待处理的呈现消息,例如 InvalidateRect 已由框架调用,将处理这些消息并重新呈现UI。

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 InvalidateRect has been called somewhere by the framework, these messages will be handled and the UI will be re-rendered.

DoEvents 一直被认为是代码气味,因为它绕过了消息队列。如果您想要响应式用户界面,则应改用辅助线程。

DoEvents has 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.

这篇关于关于在WPF中刷新UI的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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