微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Refresh / Update WPF controls

Refresh / Update WPF controls

Sometime in the past,a friend asked me how to update a control to show status while his code is doing a loop of stuff. Essentially changing the text of a label (or sophisticatedly we can say a text-based progress bar). In my past coding with MFC and WinForms,it's fairly easy enough,you just invalidate and do an update (Invalidate / UpdateWindow in MFC or Invalidate / Update in WinForms). This approach also coincides with how Windows UI operate,where you specify the region that needs to be redrawn and then you send a message to the message pump for that control to paint itself.

So,I expected something similar (if not exactly the same) to also be present in WPF; much to my surprise,there is no equivalent. All my internet searches actually shows how to do this using background thread - it is the approach that needs to be taken in a proper programming context,however there are times when you just want to do something quick & dirty or you want to augment an existing app / port where you don't want to introduce new elements. There are also considerations to be made when both UI and worker thread access the same data,especially with regard to data binding (seemy postabout collection change not supporting multi-threading out of the Box).

So,I've decided to add a helper method to refresh a WPF control. I really appreciated the Refresh method in WinForms (which executes both Invalidate & Update),so I'm renaming my method to be Refresh as well. The code snippet below also show some C# specific techniques,namely: anonymous delegates and extension methods.

publicstaticclassExtensionMethods

{

privatestaticActionEmptyDelegate =delegate() { };

publicstaticvoidRefresh(thisUIElementuiElement)

{
uiElement.dispatcher.Invoke(dispatcherPriority.Render,EmptyDelegate);
}
}

privatevoidLoopingMethod()

{
for(inti = 0; i < 10; i++)
{
label1.Content = i.ToString();
label1.Refresh();
Thread.Sleep(500);
}
}


The LoopingMethod is just the method I use in my Window class to update the label (updating the progress) and then the code does some heavy lifting (Sleep). The Refresh method is the extension method that takes any UI element and then calls that UIElement's dispatcher's Invoke method. The trick is to call the Invoke method with dispatcherPriority of Render or lower. Since we don't want to do anything,I created an empty delegate. So how come this achieves refresh functionality?

When the dispatcherPriority is set to Render (or lower),the code will then execute all operations that are of that priority or higher. In the example,the code already sets label1.Content to something else,which will result in a render operation. So by calling dispatcher.Invoke,the code essentially asks the system to execute all operations that are Render or higher priority,thus the control will then render itself (drawing the new content). Afterwards,it will then execute the provided delegate (which is our empty method).

Pretty weird; there was a post somewhere in my google search that led me this route,and I was surprised as to how it worked. I Couldn't find it anymore,but credit where credit is due,someone else figured out that Invoke-ing a Render or lower priority task will result in the UI being redrawn.

Update (January 20,2009):

A Commenter asked for a full sample,so I've uploaded onehere. I don't speak Spanish,butGoogle translatorwas working great!

Update (February 26,2009):

A Commenter asked for a VB.NET sample,so I've uploaded onehere.

Update (January 13,2010):

Apparentlygeekswithblogsdoesn't allow linking non-image files (thus the samples link doesn't work anymore). Samples are Now being put inmy Google Sites site. C# samplehereand VB.NET samplehere.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐