■ RefreshContainer 클래스의 RefreshRequested 이벤트를 사용하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestProject"> <Grid Name="grid" Margin="10"> <RefreshContainer Name="refreshContainer" VerticalAlignment="Center" HorizontalAlignment="Center"> <ListView Name="listView" BorderThickness="1" BorderBrush="{ThemeResource TextControlBorderBrush}" /> </RefreshContainer> </Grid> </Window> |
▶ MainWindow.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 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 |
using System; using System.Collections.ObjectModel; using Microsoft.UI.Dispatching; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Windows.Foundation; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 소스 컬렉션 /// </summary> private ObservableCollection<string> sourceCollection = new ObservableCollection<string>(); /// <summary> /// 디스패처 타이머 /// </summary> private DispatcherTimer timer = new DispatcherTimer(); /// <summary> /// 추가 카운트 /// </summary> private int addCount = 0; /// <summary> /// 리프레쉬 완료 연기 /// </summary> private Deferral refreshCompletionDeferral = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.timer.Interval = new TimeSpan(0, 0, 0, 0, 500); this.timer.Tick += timer_Tick; string sourceList = "AcrylicBrush ColorPicker NavigationView ParallaxView PersonPicture PullToRefreshPage RatingsControl RevealBrush TreeView"; foreach(string source in sourceList.Split(' ')) { this.sourceCollection.Add(source); } this.listView.ItemsSource = this.sourceCollection; this.refreshContainer.RefreshRequested += refreshContainer_RefreshRequested; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 타이머 틱 처리하기 - timer_Tick(sender, e) /// <summary> /// 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void timer_Tick(object sender, object e) { DispatcherQueue queue = this.refreshContainer.DispatcherQueue; if(queue.HasThreadAccess) { ProcessTimerTick(); } else { queue.TryEnqueue ( DispatcherQueuePriority.Normal, () => { ProcessTimerTick(); } ); } } #endregion #region 리프레쉬 컨테이너 리프레쉬 요청시 처리하기 - refreshContainer_RefreshRequested(sender, e) /// <summary> /// 리프레쉬 컨테이너 리프레쉬 요청시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void refreshContainer_RefreshRequested(RefreshContainer sender, RefreshRequestedEventArgs e) { this.refreshCompletionDeferral = e.GetDeferral(); this.timer.Start(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 타이머 틱 처리하기 - ProcessTimerTick() /// <summary> /// 타이머 틱 처리하기 /// </summary> private void ProcessTimerTick() { this.sourceCollection.Insert(0, "NewControl " + this.addCount++); this.timer.Stop(); if(this.refreshCompletionDeferral != null) { this.refreshCompletionDeferral.Complete(); this.refreshCompletionDeferral.Dispose(); this.refreshCompletionDeferral = null; } } #endregion } } |
※ 터치 스크린이 없어서 테스트를 할 수 없었다.