Refresh / Update WPF controls

前端之家收集整理的这篇文章主要介绍了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.

猜你在找的VB相关文章