■ RefreshContainer 클래스의 RefreshRequested 이벤트/RequestRefresh 메소드를 사용해 목록을 갱신하는 방법을 보여준다.
※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다.
※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType 태그를 None으로 추가했다.
▶ SampleItem.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 |
namespace TestProject; /// <summary> /// 샘플 항목 /// </summary> public class SampleItem { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 헤더 - Header /// <summary> /// 헤더 /// </summary> public string Header { get; set; } #endregion #region 일자 - Date /// <summary> /// 일자 /// </summary> public string Date { get; set; } #endregion #region 본문 - Body /// <summary> /// 본문 /// </summary> public string Body { get; set; } #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 52 53 54 55 56 57 58 |
<?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"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="300"> <CommandBar OverflowButtonVisibility="Collapsed"> <AppBarButton Name="refreshButton" VerticalAlignment="Center" Icon="Refresh" Label="Refresh" /> <CommandBar.Content> <TextBlock Style="{StaticResource TitleTextBlockStyle}" VerticalAlignment="Center" Text="List of items" /> </CommandBar.Content> </CommandBar> <RefreshContainer Name="refreshContainer"> <ListView Name="listView" Height="400" BorderThickness="1" BorderBrush="DarkGray" Padding="10"> <ListView.ItemTemplate> <DataTemplate x:DataType="local:SampleItem"> <Grid Height="80"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Style="{StaticResource SubtitleTextBlockStyle}" Text="{x:Bind Path=Header}" /> <TextBlock Grid.Row="1" Style="{StaticResource CaptionTextBlockStyle}" Margin="0 5 0 0" Text="{x:Bind Path=Date}" /> <TextBlock Grid.Row="2" Style="{StaticResource BodyTextBlockStyle}" Margin="0 5 0 0" Text="{x:Bind Path=Body}" /> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> </RefreshContainer> </StackPanel> </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 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 |
using System; using System.Collections.ObjectModel; using System.Threading.Tasks; using Windows.Foundation; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 샘플 항목 컬렉션 - SampleItemCollection /// <summary> /// 샘플 항목 컬렉션 /// </summary> public ObservableCollection<SampleItem> SampleItemCollection { get; set; } = new ObservableCollection<SampleItem>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.listView.ItemsSource = SampleItemCollection; Loaded += Page_Loaded; this.refreshButton.Click += refreshButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 페이지 로드시 처리하기 - Page_Loaded(sender, e) /// <summary> /// 페이지 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void Page_Loaded(object sender, RoutedEventArgs e) { Loaded -= Page_Loaded; this.refreshContainer.RefreshRequested += refreshContainer_RefreshRequested; this.refreshContainer.Visualizer.RefreshStateChanged += visualizer_RefreshStateChanged; await FetchAndInsertSampleItemAsync(2); } #endregion #region 리프레쉬 버튼 클릭시 처리하기 - refreshButton_Click(sender, e) /// <summary> /// 리프레쉬 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void refreshButton_Click(object sender, RoutedEventArgs e) { this.refreshContainer.RequestRefresh(); } #endregion #region 리프레쉬 컨테이너 갱신 요구시 처리하기 - refreshContainer_RefreshRequested(sender, e) /// <summary> /// 리프레쉬 컨테이너 갱신 요구시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void refreshContainer_RefreshRequested(RefreshContainer sender, RefreshRequestedEventArgs e) { using(Deferral deferral = e.GetDeferral()) { await FetchAndInsertSampleItemAsync(3); } } #endregion #region 비주얼라이저 갱신 상태 변경시 처리하기 - visualizer_RefreshStateChanged(sender, e) /// <summary> /// 비주얼라이저 갱신 상태 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void visualizer_RefreshStateChanged(RefreshVisualizer sender, RefreshStateChangedEventArgs e) { if(e.NewState == RefreshVisualizerState.Refreshing) { this.refreshButton.IsEnabled = false; } else { this.refreshButton.IsEnabled = true; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 다음 샘플 항목 구하기 - GetNextSampleItem() /// <summary> /// 다음 샘플 항목 구하기 /// </summary> /// <returns>다음 샘플 항목</returns> private SampleItem GetNextSampleItem() { return new SampleItem() { Header = $"Header {DateTime.Now.Second}", Date = DateTime.Now.ToLongDateString(), Body = DateTime.Now.ToLongTimeString() }; } #endregion #region 샘플 항목 삽입/조회하기 (비동기) - FetchAndInsertSampleItemAsync(updateCount) /// <summary> /// 샘플 항목 삽입/조회하기 (비동기) /// </summary> /// <param name="updateCount">업데이트 수</param> /// <returns>태스크</returns> private async Task FetchAndInsertSampleItemAsync(int updateCount) { for(int i = 0; i < updateCount; ++i) { await Task.Delay(1000); SampleItemCollection.Insert(0, GetNextSampleItem()); } } #endregion } |