■ NonVirtualizingLayout 클래스를 사용해 커스텀 비가상화 레이아웃을 만드는 방법을 보여준다.
※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다.
※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType 태그를 None으로 추가했다.
▶ CustomNonVirtualizingLayout.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
using Windows.Foundation; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace TestProject; /// <summary> /// 커스텀 비가상화 레이아웃 /// </summary> public class CustomNonVirtualizingLayout : NonVirtualizingLayout { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 측정하기 (오버라이드) - MeasureOverride(context, availableSize) /// <summary> /// 측정하기 (오버라이드) /// </summary> /// <param name="context">비가상화 레이아웃 컨텍스트</param> /// <param name="availableSize">이용 가능한 크기</param> /// <returns>측정 크기</returns> protected override Size MeasureOverride(NonVirtualizingLayoutContext context, Size availableSize) { double extentHeight = 0d; foreach(UIElement child in context.Children) { child.Measure(availableSize); extentHeight += child.DesiredSize.Height; } return new Size(availableSize.Width, extentHeight); } #endregion #region 배치하기 (오버라이트) - ArrangeOverride(context, finalSize) /// <summary> /// 배치하기 (오버라이트) /// </summary> /// <param name="context">컨텍스트</param> /// <param name="finalSize">최종 크기</param> /// <returns>배치 크기</returns> protected override Size ArrangeOverride(NonVirtualizingLayoutContext context, Size finalSize) { double offset = 0d; foreach(UIElement child in context.Children) { child.Arrange(new Rect(0, offset, finalSize.Width, child.DesiredSize.Height)); offset += child.DesiredSize.Height; } return finalSize; } #endregion } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestProject" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <ScrollViewer HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Height="300" BorderThickness="1" BorderBrush="DarkGray"> <ItemsRepeater Name="itemsRepeater"> <ItemsRepeater.Layout> <local:CustomNonVirtualizingLayout /> </ItemsRepeater.Layout> </ItemsRepeater> </ScrollViewer> </Page> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System.Collections.Generic; using Microsoft.UI.Xaml.Controls; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); List<string> list = new List<string>(); for(int i = 1; i <= 100; i++) { list.Add($"테스트 항목 {i:d03}"); } this.itemsRepeater.ItemsSource = list; } #endregion } |