简体   繁体   English

可以举个例子,说明何时应该使用UIElement.UpdateLayout()?

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

I was reading about this UpdateLayout() method in MSDN. 我正在阅读MSDN中的这个UpdateLayout()方法

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. 我已经使用Silverlight / WPF超过一年了,但我还没有使用过这种方法一次。

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). 例如,你可以在子控件上调用UpdateLayout ,以便它获得ActualHeightActualWidth ,如果你想根据它做其他事情(例如定位它,或绘制具有相应大小的东西)。

From my research, UpdateLayout seems to be a very forceful way to get all controls on the layout to update. 根据我的研究, UpdateLayout似乎是一种非常有力的方法,可以让布局上的所有控件都更新。 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. 这篇关于布局的MSDN文章应该有助于澄清事情。

However, from the above: 但是,从上面来看:

The UpdateLayout method forces a recursive layout update, and is frequently not necessary UpdateLayout方法强制进行递归布局更新,并且通常不是必需的

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. 如果UI中存在大量元素,则频繁调用InvalidateArrange,特别是对UpdateLayout,会产生显着的性能影响。 Avoid calling this method unless you absolutely require precise layout state for subsequent calls to other APIs in your code. 除非您对代码中其他API的后续调用绝对需要精确的布局状态,否则请避免调用此方法。

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. 这主要是通过内部UI布局逻辑自行完成的,您很可能永远不需要使用它。 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. 例如:如果在代码中您感觉添加了新控件或发生了某些特定事件,您可以调用此UpdateLayout()来强制重新布局控件。 In some cases WPF might consider that UI changes enough so it self calls your control's UpdateLayout(). 在某些情况下,WPF可能会认为UI已经足够变化,因此它会自动调用控件的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". 我们都犯错误......如果你发现一个控件应该“重绘”或“重新排列”它的孩子,你可以尝试通过调用它的“UpdateLayout”来强制它“刷新”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我必须使用UIElement.UpdateLayout? - Why do I have have to use UIElement.UpdateLayout? 当我使用“显示器”时,因为不能用“锁定”来完成,您能举一个简单的例子吗? - Can you give me a simple example when I am to use “monitor” because it cannot be done with “lock”? 等待直到绘制UIElement(没有updatelayout) - Wait until UIElement is drawn (without updatelayout) Uno 平台中的 UIElement.InvalidateArrange() 和 UpdateLayout() 替代方案 - UIElement.InvalidateArrange() and UpdateLayout() Alternatives in Uno Platforms 有人可以给我一个如何在C#中使用System.Monitor的简单示例吗? - Please can someone give me a simple example of how to use System.Monitor in C#? 任何人都可以通过示例给我Dotnetnuke模块本地化 - Can any one give me Dotnetnuke module localization with example 你能给我一个在.NET中导致内存碎片的例子吗? - Can you give me an example that causes memory fragmentation in .NET 任何人都可以举一个很好的例子来说明如何使用泛型类? - Can anybody give a good example of what to use generic classes for? 谁能给我一个使用BouncyCastle将.pem公共DSA密钥导入c#的示例吗? - Can anyone give me an example of using BouncyCastle to import .pem public DSA key into c#? 在模板中绑定到 UIElement 时如何使用 ElementName? - How to use ElementName when binding to UIElement within template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM