UIElement.Measure(Size) 方法


更新 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. 不過,在第一次設定階段之後,它可能會在沒有 Measure的情況下收到 Arrange 呼叫。當只影響 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.