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 值。

範例

此範例示範如何在 ArrangeOverride 實作中使用 Arrange。 基本概念是,您應該在嘗試呼叫 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);
}
'Second arrange all children and return final size of panel 
Protected Overrides Function ArrangeOverride(ByVal finalSize As Size) As Size
    'Get the collection of children 
    Dim mychildren As UIElementCollection = Children
    'Get total number of children 
    Dim count As Integer = mychildren.Count
    'Arrange children 
    'only allowing 9 children in this panel. More children will get a 0x0 layout slot. 
    Dim i As Integer
    For i = 0 To 8
        'Get (left, top) origin point for the element in the 3x3 block 
        Dim cellOrigin As Point = 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. 
        Dim dw As Double = mychildren(i).DesiredSize.Width
        Dim dh As Double = mychildren(i).DesiredSize.Height
        mychildren(i).Arrange(New Rect(cellOrigin.X, cellOrigin.Y, dw, dh))
    Next
    For i = 9 To count - 1
        'Give the remaining children a 0x0 layout slot 
        mychildren(i).Arrange(New Rect(0, 0, 0, 0))
    Next
    'Return final size of the panel 
    Return New Size(300, 300)
End Function
'Calculate point origin of the Block you are in 
Protected Function GetOrigin(ByVal blockNum As Integer, ByVal blocksPerRow As Integer, ByVal itemSize As Size) As Point
    'Get row number (zero-based) 
    Dim row As Integer = CInt(Math.Floor(blockNum / blocksPerRow))
    'Get column number (zero-based) 
    Dim column As Integer = blockNum - blocksPerRow * row
    'Calculate origin 
    Dim origin As New Point(itemSize.Width * column, itemSize.Height * row)
    Return origin
End Function

備註

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

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

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

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

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

適用於

另請參閱