简体   繁体   中英

Can give me an example that when should use UIElement.UpdateLayout()?

I was reading about this UpdateLayout() method in MSDN.

It says:

Ensures that all visual child elements of this element are properly updated for layout.

But I don't understand what is meant by layout not updated properly .

I have been working with Silverlight/WPF for over a year, but I still haven't used this method once.

So can someone give me an example that will require use of this method? So I can really understand what it does and when I should use it?

You might want to call this if you need a control to immediately complete its layout so that you can do something based on that. For example, you might call UpdateLayout on a child control so that it gets an ActualHeight and ActualWidth , if you want do do something else based on that (eg position it, or draw something with a corresponding size).

From my research, UpdateLayout seems to be a very forceful way to get all controls on the layout to update. However, in most cases, the layout system is smart enough and quick enough to update appropriately on its own. Everything that I have read leads me to believe that this call is only needed if you are doing a lot of layout modification (adding controls and moving them around) and you need one particular layout scenario to be displayed while the rest of the update stack is completed (however I still have yet to see a good example to display this...the normal layout algorithm seems to be just that good). This MSDN article on layout should help clarify things.

However, from the above:

The UpdateLayout method forces a recursive layout update, and is frequently not necessary

and this documentation

Frequent calls to InvalidateArrange, or in particular to UpdateLayout, have significant performance consequences if large numbers of elements exist in the UI. Avoid calling this method unless you absolutely require precise layout state for subsequent calls to other APIs in your code.

In conclusion to directly answer your question. This is mostly done on its own with internal UI layout logic, and you will most likely never need to use it. By the above, it seems to me that it would be pretty obvious when you do need to use it (and it is not for threading as many, including myself had thought)

I think is most used when building controls. For example: if in code you feel that a new control is added or something particular happened you might call this UpdateLayout() to force a re-layout of the controls. In some cases WPF might consider that UI changes enough so it self calls your control's UpdateLayout(). We all make mistakes... if you found a control that should "redraw" or "rearrange" its children you can try to force it to "refresh" by calling its "UpdateLayout".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM