■ VirtualizingLayout 클래스를 사용해 가상화 스택 레이아웃을 만드는 방법을 보여준다.
※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다.
※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType 태그를 None으로 추가했다.
▶ Recipe.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 |
using System; namespace TestProject; /// <summary> /// 레시피 /// </summary> public class Recipe { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이미지 URI - ImageURI /// <summary> /// 이미지 URI /// </summary> public Uri ImageURI { get; set; } #endregion #region 설명 - Description /// <summary> /// 설명 /// </summary> public string Description { get; set; } #endregion } |
▶ VirtualizingStackLayout.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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using Windows.Foundation; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml; namespace TestProject; /// <summary> /// 가상화 스택 레이아웃 /// </summary> public class VirtualizingStackLayout : VirtualizingLayout { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 평가 버퍼 리스트 /// </summary> private List<double> estimationBufferList = Enumerable.Repeat(0d, 100).ToList(); /// <summary> /// 평가용 항목 카운트 /// </summary> private int itemCountForEstimation = 0; /// <summary> /// 평가용 전체 높이 /// </summary> private double totalHeightForEstimation = 0; /// <summary> /// 첫번째 실현 데이터 인덱스 /// </summary> private int firstRealizedDataIndex = 0; /// <summary> /// 실현 엘리먼트 테두리 사각형 리스트 /// </summary> private List<Rect> realizedElementBoundRectangleList = new List<Rect>(); /// <summary> /// 마지막 범위 사각형 /// </summary> Rect lastExtentRectangle = new Rect(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 측정하기 (오버라이드) - MeasureOverride(context, availableSize) /// <summary> /// 측정하기 (오버라이드) /// </summary> /// <param name="context">가상화 레이아웃 컨텍스트</param> /// <param name="availableSize">이용 가능한 크기</param> /// <returns>측정 크기</returns> protected override Size MeasureOverride(VirtualizingLayoutContext context, Size availableSize) { Rect viewportRectangle = context.RealizationRect; RemoveCachedBoundRectangleOutsideViewport(viewportRectangle); int startIndex = GetStartIndex(context, availableSize); Generate(context, availableSize, startIndex, forward : true ); Generate(context, availableSize, startIndex, forward : false); this.lastExtentRectangle = EstimateExtent(context, availableSize); context.LayoutOrigin = new Point(this.lastExtentRectangle.X, this.lastExtentRectangle.Y); return new Size(this.lastExtentRectangle.Width, this.lastExtentRectangle.Height); } #endregion #region 배치하기 (오버라이드) - ArrangeOverride(context, finalSize) /// <summary> /// 배치하기 (오버라이드) /// </summary> /// <param name="context">가상화 레이아웃 컨텍스트</param> /// <param name="finalSize">촤종 크기</param> /// <returns>배치 크기</returns> protected override Size ArrangeOverride(VirtualizingLayoutContext context, Size finalSize) { for(int realizationIndex = 0; realizationIndex < this.realizedElementBoundRectangleList.Count; realizationIndex++) { int currentDataIndex = this.firstRealizedDataIndex + realizationIndex; UIElement childElement = context.GetOrCreateElementAt(currentDataIndex); Rect arrangeBoundRectangle = this.realizedElementBoundRectangleList[realizationIndex]; arrangeBoundRectangle.X -= this.lastExtentRectangle.X; arrangeBoundRectangle.Y -= this.lastExtentRectangle.Y; childElement.Arrange(arrangeBoundRectangle); } return finalSize; } #endregion #region 항목 컬렉션 변경시 처리하기 (코어) - OnItemsChangedCore(context, source, e) /// <summary> /// 항목 컬렉션 변경시 처리하기 (코어) /// </summary> /// <param name="context">가상화 레이아웃 컨텍스트</param> /// <param name="source">소스 객체</param> /// <param name="e">이벤트 인자</param> /// <remarks> /// 데이터 컬렉션이 변경되었다. /// 뷰포트에서 요소의 경계를 유지하고 있으므로 컬렉션 변경을 고려하여 목록을 업데이트한다. /// </remarks> protected override void OnItemsChangedCore(VirtualizingLayoutContext context, object source, NotifyCollectionChangedEventArgs e) { InvalidateMeasure(); if(this.realizedElementBoundRectangleList.Count > 0) { switch(e.Action) { case NotifyCollectionChangedAction.Add : OnItemsAdded(e.NewStartingIndex, e.NewItems.Count); break; case NotifyCollectionChangedAction.Replace : OnItemsRemoved(e.OldStartingIndex, e.OldItems.Count); OnItemsAdded(e.NewStartingIndex, e.NewItems.Count); break; case NotifyCollectionChangedAction.Remove : OnItemsRemoved(e.OldStartingIndex, e.OldItems.Count); break; case NotifyCollectionChangedAction.Reset : this.realizedElementBoundRectangleList.Clear(); this.firstRealizedDataIndex = 0; break; default : throw new NotImplementedException(); } } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 사각형 교차 여부 구하기 - IsRectangleIntersected(boundRectangle, viewportRectangle) /// <summary> /// 사각형 교차 여부 구하기 /// </summary> /// <param name="boundRectangle">테두리 사각형</param> /// <param name="viewportRectangle">뷰포트 사각형</param> /// <returns>사각형 교차 여부</returns> private bool IsRectangleIntersected(Rect boundRectangle, Rect viewportRectangle) { return !(boundRectangle.Bottom < viewportRectangle.Top || boundRectangle.Top > viewportRectangle.Bottom); } #endregion #region 뷰포트 외부 캐시 테두리 사각형 제거하기 - RemoveCachedBoundRectangleOutsideViewport(viewportRectangle) /// <summary> /// 뷰포트 외부 캐시 테두리 사각형 제거하기 /// </summary> /// <param name="viewportRectangle">뷰포트 사각형</param> private void RemoveCachedBoundRectangleOutsideViewport(Rect viewportRectangle) { int firstRealizedIndexInViewport = 0; while(firstRealizedIndexInViewport < this.realizedElementBoundRectangleList.Count && !IsRectangleIntersected(this.realizedElementBoundRectangleList[firstRealizedIndexInViewport], viewportRectangle)) { firstRealizedIndexInViewport++; } int lastRealizedIndexInViewport = this.realizedElementBoundRectangleList.Count - 1; while(lastRealizedIndexInViewport >= 0 && !IsRectangleIntersected(this.realizedElementBoundRectangleList[lastRealizedIndexInViewport], viewportRectangle)) { lastRealizedIndexInViewport--; } if(firstRealizedIndexInViewport > 0) { this.firstRealizedDataIndex += firstRealizedIndexInViewport; this.realizedElementBoundRectangleList.RemoveRange(0, firstRealizedIndexInViewport); } if(lastRealizedIndexInViewport >= 0 && lastRealizedIndexInViewport < this.realizedElementBoundRectangleList.Count - 2) { this.realizedElementBoundRectangleList.RemoveRange ( lastRealizedIndexInViewport + 2, this.realizedElementBoundRectangleList.Count - lastRealizedIndexInViewport - 3 ); } } #endregion #region 실현 여부 구하기- IsRealized(dataIndex) /// <summary> /// 실현 여부 구하기 /// </summary> /// <param name="dataIndex">데이터 인덱스</param> /// <returns>실현 여부</returns> private bool IsRealized(int dataIndex) { int realizationIndex = dataIndex - this.firstRealizedDataIndex; return realizationIndex >= 0 && realizationIndex < this.realizedElementBoundRectangleList.Count; } #endregion #region 실현 범위 지우기 - ClearRealizedRange() /// <summary> /// 실현 범위 지우기 /// </summary> private void ClearRealizedRange() { this.realizedElementBoundRectangleList.Clear(); this.firstRealizedDataIndex = 0; } #endregion #region 뷰포트에서 첫번째 실현 데이터 인덱스 구하기 - GetFirstRealizedDataIndexInViewport(viewportRectangle) /// <summary> /// 뷰포트에서 첫번째 실현 데이터 인덱스 구하기 /// </summary> /// <param name="viewportRectangle">뷰포트 사각형</param> /// <returns>첫번째 실현 데이터 인덱스</returns> private int GetFirstRealizedDataIndexInViewport(Rect viewportRectangle) { int index = -1; if(this.realizedElementBoundRectangleList.Count > 0) { for(int i = 0; i < this.realizedElementBoundRectangleList.Count; i++) { if(this.realizedElementBoundRectangleList[i].Y < viewportRectangle.Bottom && this.realizedElementBoundRectangleList[i].Bottom > viewportRectangle.Top) { index = this.firstRealizedDataIndex + i; break; } } } return index; } #endregion #region 뷰포트용 평가 인덱스 구하기 - GetEstimateIndexForViewport(viewportRectangle, dataCount) /// <summary> /// 뷰포트용 평가 인덱스 구하기 /// </summary> /// <param name="viewportRectangle">뷰포트 사각형</param> /// <param name="dataCount">데이터 카운트</param> /// <returns>평가 인덱스</returns> private int GetEstimateIndexForViewport(Rect viewportRectangle, int dataCount) { double averageHeight = this.totalHeightForEstimation / this.itemCountForEstimation; int estimatedIndex = (int)(viewportRectangle.Top / averageHeight); estimatedIndex = Math.Max(0, Math.Min(estimatedIndex, dataCount)); return estimatedIndex; } #endregion #region 실현 여부 보장하기 - EnsureRealized(dataIndex) /// <summary> /// 실현 여부 보장하기 /// </summary> /// <param name="dataIndex">데이터 인덱스</param> /// <returns>실현 여부</returns> private bool EnsureRealized(int dataIndex) { if(!IsRealized(dataIndex)) { int realizationIndex = GetRealizationIndex(dataIndex); if(realizationIndex == -1) { this.realizedElementBoundRectangleList.Insert(0, new Rect()); } else { this.realizedElementBoundRectangleList.Add(new Rect()); } if(this.firstRealizedDataIndex > dataIndex) { this.firstRealizedDataIndex = dataIndex; } return true; } return false; } #endregion #region 엘리먼트 측정하기 - MeasureElement(context, index, availableSize) /// <summary> /// 엘리먼트 측정하기 /// </summary> /// <param name="context">가상화 레이아웃 컨텍스트</param> /// <param name="index">인덱스</param> /// <param name="availableSize">이용 가능한 크기</param> /// <returns>측정 크기</returns> private Size MeasureElement(VirtualizingLayoutContext context, int index, Size availableSize) { UIElement child = context.GetOrCreateElementAt(index); child.Measure(availableSize); int estimationBufferIndex = index % this.estimationBufferList.Count; bool alreadyMeasured = this.estimationBufferList[estimationBufferIndex] != 0; if(!alreadyMeasured) { this.itemCountForEstimation++; } this.totalHeightForEstimation -= this.estimationBufferList[estimationBufferIndex]; this.totalHeightForEstimation += child.DesiredSize.Height; this.estimationBufferList[estimationBufferIndex] = child.DesiredSize.Height; return child.DesiredSize; } #endregion #region 실현 인덱스 구하기 - GetRealizationIndex(dataIndex) /// <summary> /// 실현 인덱스 구하기 /// </summary> /// <param name="dataIndex">데이터 인덱스</param> /// <returns>실현 인덱스</returns> private int GetRealizationIndex(int dataIndex) { return dataIndex - this.firstRealizedDataIndex; } #endregion #region 데이터 인덱스용 캐시 테두리 사각형 구하기 - GetCachedBoundRectangleForDataIndex(dataIndex) /// <summary> /// 데이터 인덱스용 캐시 테두리 사각형 구하기 /// </summary> /// <param name="dataIndex">데이터 인덱스</param> /// <returns>데이터 인덱스용 캐시 테두리 사각형</returns> private Rect GetCachedBoundRectangleForDataIndex(int dataIndex) { return this.realizedElementBoundRectangleList[GetRealizationIndex(dataIndex)]; } #endregion #region 데이터 인덱스용 캐시 테두리 사각형 설정하기 - SetCachedBoundsForDataIndex(dataIndex, boundRectangle) /// <summary> /// 데이터 인덱스용 캐시 테두리 사각형 설정하기 /// </summary> /// <param name="dataIndex">데이터 인덱스</param> /// <param name="boundRectangle">테두리 사각형</param> private void SetCachedBoundRectangleForDataIndex(int dataIndex, Rect boundRectangle) { this.realizedElementBoundRectangleList[GetRealizationIndex(dataIndex)] = boundRectangle; } #endregion #region 시작 인덱스 구하기 - GetStartIndex(context, availableSize) /// <summary> /// 시작 인덱스 구하기 /// </summary> /// <param name="context">가상화 레이아웃 컨텍스트</param> /// <param name="availableSize">이용 가능한 크기</param> /// <returns>시작 인덱스</returns> private int GetStartIndex(VirtualizingLayoutContext context, Size availableSize) { int startDataIndex = -1; int recommendedAnchorIndex = context.RecommendedAnchorIndex; bool isSuggestedAnchorValid = recommendedAnchorIndex != -1; if(isSuggestedAnchorValid) { if(IsRealized(recommendedAnchorIndex)) { startDataIndex = recommendedAnchorIndex; } else { ClearRealizedRange(); startDataIndex = recommendedAnchorIndex; } } else { startDataIndex = GetFirstRealizedDataIndexInViewport(context.RealizationRect); if(startDataIndex < 0) { startDataIndex = GetEstimateIndexForViewport(context.RealizationRect, context.ItemCount); ClearRealizedRange(); } } if(startDataIndex != -1 & context.ItemCount > 0) { if(this.realizedElementBoundRectangleList.Count == 0) { this.firstRealizedDataIndex = startDataIndex; } bool newAnchor = EnsureRealized(startDataIndex); Size desiredSize = MeasureElement(context, startDataIndex, availableSize); Rect boundRectangle = new Rect ( 0, newAnchor ? (this.totalHeightForEstimation / this.itemCountForEstimation) * startDataIndex : GetCachedBoundRectangleForDataIndex(startDataIndex).Y, availableSize.Width, desiredSize.Height ); SetCachedBoundRectangleForDataIndex(startDataIndex, boundRectangle); } return startDataIndex; } #endregion #region 데이터 인덱스 무결성 여부 구하기 - IsDataIndexValid(currentDataIndex, itemCount) /// <summary> /// 데이터 인덱스 무결성 여부 구하기 /// </summary> /// <param name="currentDataIndex">현재 데이터 인덱스</param> /// <param name="itemCount">항목 카운트</param> /// <returns>데이터 인덱스 무결성 여부</returns> private bool IsDataIndexValid(int currentDataIndex, int itemCount) { return currentDataIndex >= 0 && currentDataIndex < itemCount; } #endregion #region 공간 채우기 계속 여부 구하기 - ShouldContinueFillingUpSpace(dataIndex, forward, viewportRectangle) /// <summary> /// 공간 채우기 계속 여부 구하기 /// </summary> /// <param name="dataIndex">데이터 인덱스</param> /// <param name="forward">진행 여부</param> /// <param name="viewportRectangle">뷰포트 사각형</param> /// <returns>공간 채우기 계속 여부</returns> private bool ShouldContinueFillingUpSpace(int dataIndex, bool forward, Rect viewportRectangle) { Rect boundRectangle = GetCachedBoundRectangleForDataIndex(dataIndex); return forward ? boundRectangle.Y < viewportRectangle.Bottom : boundRectangle.Y > viewportRectangle.Top; } #endregion #region 생성하기 - Generate(context, availableSize, anchorDataIndex, forward) /// <summary> /// 생성하기 /// </summary> /// <param name="context">가상화 레이아웃 컨텍스트</param> /// <param name="availableSize">이용 가능한 크기</param> /// <param name="anchorDataIndex">앵커 데이터 인덱스</param> /// <param name="forward">진행 여부</param> private void Generate(VirtualizingLayoutContext context, Size availableSize, int anchorDataIndex, bool forward) { int step = forward ? 1 : -1; int previousDataIndex = anchorDataIndex; int currentDataIndex = previousDataIndex + step; Rect viewportRectangle = context.RealizationRect; while(IsDataIndexValid(currentDataIndex, context.ItemCount) && ShouldContinueFillingUpSpace(previousDataIndex, forward, viewportRectangle)) { EnsureRealized(currentDataIndex); Size desiredSize = MeasureElement(context, currentDataIndex, availableSize); Rect previousBoundRectangle = GetCachedBoundRectangleForDataIndex(previousDataIndex); Rect currentBoundRectangle = new Rect ( 0, forward ? previousBoundRectangle.Y + previousBoundRectangle.Height : previousBoundRectangle.Y - desiredSize.Height, availableSize.Width, desiredSize.Height) ; SetCachedBoundRectangleForDataIndex(currentDataIndex, currentBoundRectangle); previousDataIndex = currentDataIndex; currentDataIndex += step; } } #endregion #region 범위 평가하기 - EstimateExtent(context, availableSize) /// <summary> /// 범위 평가하기 /// </summary> /// <param name="context">가상화 레이아웃 컨텍스트</param> /// <param name="availableSize">이용 가능한 크기</param> /// <returns>범위 평가 사각형</returns> private Rect EstimateExtent(VirtualizingLayoutContext context, Size availableSize) { double averageHeight = this.totalHeightForEstimation / this.itemCountForEstimation; Rect extentRectangle = new Rect(0, 0, availableSize.Width, context.ItemCount * averageHeight); if(context.ItemCount > 0 && this.realizedElementBoundRectangleList.Count > 0) { extentRectangle.Y = this.firstRealizedDataIndex == 0 ? this.realizedElementBoundRectangleList[0].Y : this.realizedElementBoundRectangleList[0].Y - (this.firstRealizedDataIndex - 1) * averageHeight; int lastRealizedIndex = this.firstRealizedDataIndex + this.realizedElementBoundRectangleList.Count; if(lastRealizedIndex == context.ItemCount - 1) { Rect lastBoundRectangle = this.realizedElementBoundRectangleList[this.realizedElementBoundRectangleList.Count - 1]; extentRectangle.Y = lastBoundRectangle.Bottom; } else { Rect lastBoundRectangle = realizedElementBoundRectangleList[this.realizedElementBoundRectangleList.Count - 1]; int lastRealizedDataIndex = this.firstRealizedDataIndex + this.realizedElementBoundRectangleList.Count; int itemCountAfterLastRealizedIndex = context.ItemCount - lastRealizedDataIndex; extentRectangle.Height = lastBoundRectangle.Bottom + itemCountAfterLastRealizedIndex * averageHeight - extentRectangle.Y; } } return extentRectangle; } #endregion #region 실현 범위 지우기 - ClearRealizedRange(startRealizedIndex, count) /// <summary> /// 실현 범위 지우기 /// </summary> /// <param name="startRealizedIndex">시작 실현 인덱스</param> /// <param name="count">카운트</param> private void ClearRealizedRange(int startRealizedIndex, int count) { this.realizedElementBoundRectangleList.RemoveRange(startRealizedIndex, count); if(startRealizedIndex == 0) { this.firstRealizedDataIndex = this.realizedElementBoundRectangleList.Count == 0 ? 0 : this.firstRealizedDataIndex + count; } } #endregion #region 항목 추가시 처리하기 - OnItemsAdded(index, count) /// <summary> /// 항목 추가시 처리하기 /// </summary> /// <param name="index">인덱스</param> /// <param name="count">카운트</param> private void OnItemsAdded(int index, int count) { int lastRealizedDataIndex = this.firstRealizedDataIndex + this.realizedElementBoundRectangleList.Count - 1; int newStartingIndex = index; if(newStartingIndex > this.firstRealizedDataIndex && newStartingIndex <= lastRealizedDataIndex) { int insertRangeStartIndex = newStartingIndex - this.firstRealizedDataIndex; for(int i = 0; i < count; i++) { int insertRangeIndex = insertRangeStartIndex + i; int dataIndex = newStartingIndex + i; this.realizedElementBoundRectangleList.Insert(insertRangeIndex, new Rect()); } } else if(index <= this.firstRealizedDataIndex) { this.firstRealizedDataIndex += count; } } #endregion #region 항목 제거시 처리하기 - OnItemsRemoved(index, count) /// <summary> /// 항목 제거시 처리하기 /// </summary> /// <param name="index">인덱스</param> /// <param name="count">카운트</param> private void OnItemsRemoved(int index, int count) { int lastRealizedDataIndex = this.firstRealizedDataIndex + this.realizedElementBoundRectangleList.Count - 1; int startIndex = Math.Max(this.firstRealizedDataIndex, index); int endIndex = Math.Min(lastRealizedDataIndex, index + count - 1); bool removeAffectsFirstRealizedDataIndex = (index <= this.firstRealizedDataIndex); if(endIndex >= startIndex) { ClearRealizedRange(GetRealizationIndex(startIndex), endIndex - startIndex + 1); } if(removeAffectsFirstRealizedDataIndex && this.firstRealizedDataIndex != -1) { this.firstRealizedDataIndex -= count; } } #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 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 |
<?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> <ItemsRepeater Name="itemsRepeater"> <ItemsRepeater.Layout> <local:VirtualizingStackLayout /> </ItemsRepeater.Layout> <ItemsRepeater.ItemTemplate> <DataTemplate x:DataType="local:Recipe"> <UserControl Margin="10" IsTabStop="True" UseSystemFocusVisuals="True"> <Grid BorderThickness="1" Background="LightGray"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Grid.Column="0" VerticalAlignment="Center" Margin="10" Width="150" Height="150"> <Image.Source> <BitmapImage DecodePixelHeight="100" UriSource="{x:Bind ImageURI}" /> </Image.Source> </Image> <TextBlock Grid.Column="1" VerticalAlignment="Center" Margin="10" TextWrapping="Wrap" Text="{x:Bind Description}" /> </Grid> </UserControl> </DataTemplate> </ItemsRepeater.ItemTemplate> </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 38 39 40 41 42 43 44 45 46 47 48 |
using System; using System.Collections.ObjectModel; using System.Linq; using Microsoft.UI.Xaml.Controls; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); string loremText = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam laoreet erat vel massa rutrum, eget mollis massa vulputate. Vivamus semper augue leo, eget faucibus nulla mattis nec. Donec scelerisque lacus at dui ultricies, eget auctor ipsum placerat. Integer aliquet libero sed nisi eleifend, nec rutrum arcu lacinia. Sed a sem et ante gravida congue sit amet ut augue. Donec quis pellentesque urna, non finibus metus. Proin sed ornare tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam laoreet erat vel massa rutrum, eget mollis massa vulputate. Vivamus semper augue leo, eget faucibus nulla mattis nec. Donec scelerisque lacus at dui ultricies, eget auctor ipsum placerat. Integer aliquet libero sed nisi eleifend, nec rutrum arcu lacinia. Sed a sem et ante gravida congue sit amet ut augue. Donec quis pellentesque urna, non finibus metus. Proin sed ornare tellus."; Random random = new Random(); ObservableCollection<Recipe> recipeCollection = new ObservableCollection<Recipe> ( Enumerable.Range(0, 300).Select ( k => new Recipe { ImageURI = new Uri(string.Format("ms-appx:///Assets/LandscapeImage{0}.jpg", k % 8 + 1)), Description = k + " - " + loremText.Substring(0, random.Next(50, 350)) } ) ); this.itemsRepeater.ItemsSource = recipeCollection; } #endregion } |