UIElement.Measure(Size) Method


更新 UIElementDesiredSizeUpdates the DesiredSize of a UIElement. 父元素从其自身的 MeasureCore(Size) 实现调用此方法以形成递归布局更新。Parent elements call this method from their own MeasureCore(Size) implementations to form a recursive layout update. 调用此方法构成布局更新的第一个处理过程(“测量”处理过程)。Calling this method constitutes the first pass (the "Measure" pass) of a layout update.

 void Measure(System::Windows::Size availableSize);
public void Measure (System.Windows.Size availableSize);
member this.Measure : System.Windows.Size -> unit
Public Sub Measure (availableSize As Size)



父元素可以为子元素分配的可用空间。The available space that a parent element can allocate a child element. 子元素可以请求大于可用空间的空间;如果可在当前元素的内容模型中进行滚动,则提供的大小可以适应此空间。A child element can request a larger space than what is available; the provided size might be accommodated if scrolling is possible in the content model for the current element.


Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 中的布局定位的计算由 Measure 调用和 Arrange 调用组成。Computation of layout positioning in Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) is comprised of a Measure call and an Arrange call. Measure 调用期间,元素通过使用 availableSize 输入来确定其大小要求。During the Measure call, an element determines its size requirements by using an availableSize input. Arrange 调用期间,元素大小已完成。During the Arrange call, the element size is finalized.

availableSize 可以是从零到无限大的任何数字。availableSize can be any number from zero to infinite. 参与布局的元素应返回给定 availableSize所需的最小 SizeElements participating in layout should return the minimum Size they require for a given availableSize.

第一次实例化布局时,将始终在 Arrange之前接收 Measure 调用。When a layout is first instantiated, it always receives a Measure call before Arrange. 但是,在第一次布局传递后,它可能会收到 Arrange 调用,但没有 Measure;如果更改仅影响 Arrange 的属性(如对齐方式),或者当父对象收到没有 MeasureArrange 时,会发生这种情况。However, after the first layout pass, it may receive an Arrange call without a Measure; this can happen when a property that affects only Arrange is changed (such as alignment), or when the parent receives an Arrange without a Measure. Measure 调用会自动使 Arrange 调用无效。A Measure call will automatically invalidate an Arrange call.

布局更新以异步方式进行,因此主线程不会等待每个可能的布局更改。Layout updates happen asynchronously, such that the main thread is not waiting for every possible layout change. 通过对属性值进行代码隐藏检查来查询元素,可能不会立即反映对与大小调整或布局特征(例如 Width 属性)交互的属性所做的更改。Querying an element via code-behind checking of property values may not immediately reflect changes to properties that interact with the sizing or layout characteristics (the Width property, for example).


可以使用 UpdateLayout 方法强制执行布局更新。Layout updates can be forced by using the UpdateLayout method. 但是,通常不需要调用此方法,这可能会导致性能不佳。However, calling this method is usually unnecessary and can cause poor performance.

布局系统将两个不同的队列(一个用于 Measure,一个用于 ArrangeThe layout system keeps two separate queues of invalid layouts, one for Measure and one for Arrange. 布局队列是根据执行布局的元素的可视化树中的元素顺序进行排序的。树中较高位置的元素位于队列的顶部,以避免父代中的重复更改导致冗余的布局。The layout queue is sorted based upon the order of elements in the visual tree of the element performing layout; elements higher in the tree are at the top of the queue, to avoid redundant layouts caused by repeated changes in parents. 重复项会自动从队列中删除,如果元素已经进行了布局验证,则会自动从队列中删除它们。Duplicate entries are automatically removed from the queue, and elements are automatically removed from the queue if they are already layout-validated.

更新布局时,首先清空 Measure 队列,后跟 Arrange 队列。When updating layout, the Measure queue is emptied first, followed by the Arrange queue. 如果 Measure 队列中有元素,则永远不会排列 Arrange 队列中的元素。An element in the Arrange queue will never be arranged if there is an element in the Measure queue.

Applies to