UIElement.Arrange(Rect) 方法

定義

放置子物件,並決定 UIElement的大小。 為其子專案實作自訂配置的父物件應該從其版面配置覆寫實作呼叫這個方法,以形成遞迴配置更新。

public:
 virtual void Arrange(Rect finalRect) = Arrange;
void Arrange(Rect const& finalRect);
public void Arrange(Rect finalRect);
function arrange(finalRect)
Public Sub Arrange (finalRect As Rect)

參數

finalRect
Rect

父系在配置中計運算元系的最終大小,提供為 Rect 值。

範例

此範例示範如何在 ArrangeArrangeOverride 實作中使用 。 基本概念是,您應該在嘗試呼叫 Arrange 的任何專案上查詢DesiredSize,以便您具有 的值 finalRect ,除非您的版面配置實作有一些特定設計,會改變或忽略所需的大小,然後再將其傳遞為 finalRect

// Second arrange all children and return final size of panel
protected override Size ArrangeOverride(Size finalSize)
{
    // Get the collection of children
    UIElementCollection mychildren = Children;

    // Get total number of children
    int count = mychildren.Count;

    // Arrange children
    // We're only allowing 9 children in this panel.  More children will get a 0x0 layout slot.
    int i;
    for (i = 0; i < 9; i++)
    {

        // Get (left, top) origin point for the element in the 3x3 block
        Point cellOrigin = GetOrigin(i, 3, new Size(100, 100));

        // Arrange child
        // Get desired height and width. This will not be larger than 100x100 as set in MeasureOverride.
        double dw = mychildren[i].DesiredSize.Width;
        double dh = mychildren[i].DesiredSize.Height;

        mychildren[i].Arrange(new Rect(cellOrigin.X, cellOrigin.Y, dw, dh));

    }

    // Give the remaining children a 0x0 layout slot
    for (i = 9; i < count; i++)
    {
        mychildren[i].Arrange(new Rect(0, 0, 0, 0));
    }


    // Return final size of the panel
    return new Size(300, 300);
}

備註

呼叫 Arrange 可能會達到該特定類別的 ArrangeOverride 實作。 否則,大部分 的 FrameworkElement 類別都有 的 Arrange 隱含預設配置行為。

在 XAML UI 中計算初始版面配置定位是由 Measure 呼叫和 Arrange 呼叫所組成,順序為 。 在 Measure 呼叫期間,配置系統會使用 availableSize 度量來決定元素的大小需求。 在 Arrange 呼叫期間,配置系統會完成元素周框方塊的大小和位置。

第一次產生版面配置時,它一律會有 之前 Arrange 發生的 Measure 呼叫。 不過,在第一個 Arrange 版面設定階段之後,呼叫可能會發生,而不 Measure 需在前面。 當影響的屬性只會 Arrange 變更 (例如對齊) ,或父系收到 Arrange 不含 Measure 的 時,就會發生此情況。

量值呼叫會自動使任何 Arrange 資訊失效。 版面配置更新通常會以非同步方式 (,一次由配置系統) 決定。 元素可能不會立即反映影響元素大小調整 (的屬性變更,例如 Width) 。

版面配置更新可由應用程式程式碼強制執行,而不是使用 UpdateLayout 方法依賴內建配置系統行為。 不過,不建議這麼做。 這通常是不必要的,如果過度使用,可能會導致效能不佳。 在許多情況下,從應用程式程式碼呼叫 UpdateLayout 可能會因為屬性變更而適用,配置系統可能已經處理更新。 版面配置系統也有優化,可透過父子式關聯性處理配置變更的串聯,而且呼叫 UpdateLayout 可以針對這類優化運作。 不過,在更複雜的案例中,可能會有配置情況,其中呼叫 UpdateLayout 是解決計時問題或其他配置問題的最佳選項。 只要刻意且謹慎地使用它即可。

適用於

另請參閱