UIElement.Arrange(Rect) 메서드

정의

자식 개체의 위치를 지정하고 UIElement. 자식 요소에 대한 사용자 지정 레이아웃을 구현하는 부모 개체는 레이아웃 재정의 구현에서 이 메서드를 호출하여 재귀 레이아웃 업데이트를 구성해야 합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.UIElement.Arrange(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

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 사용하는 방법을 보여줍니다. 기본 개념은 레이아웃 구현에 finalRect 로 전달하기 전에 원하는 크기를 변경하거나 무시하는 특정 디자인이 없으면 finalRect 에 대한 값을 갖도록 정렬을 호출하려는 모든 항목을 쿼리 DesiredSize 해야 한다는 것입니다.

// 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 클래스에는 정렬에 대한 암시적 기본 레이아웃 동작이 있습니다.

XAML UI의 초기 레이아웃 위치 계산은 Measure 호출 및 정렬 호출 순서로 구성됩니다. 호출 중에 Measure 레이아웃 시스템은 availableSize 측정값을 사용하여 요소의 크기 요구 사항을 결정합니다. 정렬 호출 중에 레이아웃 시스템은 요소 경계 상자의 크기와 위치를 마무리합니다.

레이아웃이 처음 생성되면 정렬 전에 항상 Measure 호출이 발생합니다. 그러나 첫 번째 레이아웃이 통과한 후 정렬 호출은 앞에 없으면 Measure 발생할 수 있습니다. 정렬에만 영향을 주는 속성이 변경되거나(예: 맞춤) 부모가 정렬 없이 Measure정렬을 받을 때 발생할 수 있습니다.

호출은 Measure 정렬 정보를 자동으로 무효화합니다. 레이아웃 업데이트는 일반적으로 비동기적으로 발생합니다(레이아웃 시스템에 의해 결정된 시간에). 요소 크기 조정(예: Width)에 영향을 주는 속성의 변경 내용을 요소에 즉시 반영하지 않을 수 있습니다.

레이아웃 업데이트는 메서드를 사용하여 UpdateLayout 기본 제공 레이아웃 시스템 동작에 의존하지 않고 앱 코드에서 강제 적용할 수 있습니다. 그러나 권장되지 않습니다. 일반적으로 불필요하며 과도하게 사용하면 성능이 저하될 수 있습니다. 속성 변경으로 인해 앱 코드에서 호출 UpdateLayout 하는 것이 적절할 수 있는 많은 상황에서 레이아웃 시스템은 이미 업데이트를 처리하고 있을 것입니다. 레이아웃 시스템에는 부모-자식 관계를 통해 레이아웃 변경의 연속을 처리하기 위한 최적화가 있으며 호출 UpdateLayout 은 이러한 최적화에 대해 작동할 수 있습니다. 그럼에도 불구하고 호출이 타이밍 문제 또는 다른 레이아웃 문제를 해결하는 가장 좋은 옵션인 더 복잡한 시나리오 UpdateLayout 에서 레이아웃 상황이 존재할 수 있습니다. 의도적으로 그리고 아쉽게 사용하세요.

적용 대상

추가 정보