Delay rendering of a custom control in WPF
I have a WPF custom control that sometimes takes a while to render in some complex scenarios. I would like to be able to tell my custom control to show a placeholder image (e.g. "Please wait - rendering!") and then actually render the control in idle time (after input is processed).
I am thinking along the lines开发者_运维知识库 of having my custom control modified to hold a parent grid and two children: A) a placeholder image and B) the actual content. By default A) is visible and when visibility of the custom control changes I could BeginInvoke a delegate which would in turn show B) instead of A).
If there are many/several such control instances on the screen they would hopefully not block the main thread while rendering all of them simultaneously. Since delegates are queued with priority lower than input priority, UI should in theory remain very responsive.
Has anybody ever encountered a similar problem?
Yes, this approach will work fine and I've used it on several occasions.
Typically, I use this approach if the control needs to retrieve and/or process data prior to rendering. I recommend doing all such work on a background thread and then invoking the actual render update using the element's Dispatcher.
Tip: I will often declare an internal DP called something like Revision of type int and register it with AffectsRender. Then once the background thread has done its heavy processing, it can BeginInvoke() a call to increment the Revision property, thereby invalidating render and causing a new render pass. You can then do the appropriate rendering logic in your OnRender override, as usual.
精彩评论