■ 백그라운드 태스크를 사용하는 방법을 보여준다.
▶ NotifyType.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
namespace TestProject { /// <summary> /// 통지 타입 /// </summary> public enum NotifyType { /// <summary> /// 상태 메시지 /// </summary> StatusMessage, /// <summary> /// 에러 메시지 /// </summary> ErrorMessage }; } |
▶ Scenario.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 |
using System; namespace TestProject { /// <summary> /// 시나리오 /// </summary> public class Scenario { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제목 - Title /// <summary> /// 제목 /// </summary> public string Title { get; set; } #endregion #region 클래스 타입 - ClassType /// <summary> /// 클래스 타입 /// </summary> public Type ClassType { get; set; } #endregion } } |
▶ BackgroundTaskHelper.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 |
using System; using System.Collections.Generic; using Windows.ApplicationModel.Background; using Windows.Foundation; using Windows.Storage; namespace TestProject { /// <summary> /// 백그라운드 태스크 헬퍼 /// </summary> public static class BackgroundTaskHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 백그라운드 태스크 진입점 1 /// </summary> public const string BackgroundTaskEntryPoint1 = "TestLibrary.SampleBackgroundTask1"; /// <summary> /// 백그라운드 태스크명 1 /// </summary> public const string BackgroundTaskName1 = "BackgroundTask1"; /// <summary> /// 백그라운드 태스크 진행 1 /// </summary> public static string BackgroundTaskProgress1 = string.Empty; /// <summary> /// 백그라운드 태스크 등록 여부 1 /// </summary> public static bool BackgroundTaskRegistered1 = false; /// <summary> /// 백그라운드 태스크 진입점 2 /// </summary> public const string BackgroundTaskEntryPoint2 = "TestLibrary.SampleBackgroundTask1"; /// <summary> /// 백그라운드 태스크명 2 /// </summary> public const string BackgroundTaskName2 = "BackgroundTask2"; /// <summary> /// 백그라운드 태스크 진행 2 /// </summary> public static string BackgroundTaskProgress2 = string.Empty; /// <summary> /// 백그라운드 태스크 등록 여부 2 /// </summary> public static bool BackgroundTaskRegistered2 = false; /// <summary> /// 백그라운드 태스크 진입점 3 /// </summary> public const string BackgroundTaskEntryPoint3 = "TestLibrary.SampleBackgroundTask2"; /// <summary> /// 백그라운드 태스크명 3 /// </summary> public const string BackgroundTaskName3 = "BackgroundTask3"; /// <summary> /// 백그라운드 태스크 진행 3 /// </summary> public static string BackgroundTaskProgress3 = string.Empty; /// <summary> /// 백그라운드 태스크 등록 여부 3 /// </summary> public static bool BackgroundTaskRegistered3 = false; /// <summary> /// 백그라운드 태스크 진입점 4 /// </summary> public const string BackgroundTaskEntryPoint4 = "TestLibrary.SampleBackgroundTask1"; /// <summary> /// 백그라운드 태스크명 4 /// </summary> public const string BackgroundTaskName4 = "BackgroundTask4"; /// <summary> /// 백그라운드 태스크 진행 4 /// </summary> public static string BackgroundTaskProgress4 = string.Empty; /// <summary> /// 백그라운드 태스크 등록 여부 4 /// </summary> public static bool BackgroundTaskRegistered4 = false; /// <summary> /// 백그라운드 태스크 진입점 5 /// </summary> public const string BackgroundTaskEntryPoint5 = "TestLibrary.SampleBackgroundTask1"; /// <summary> /// 백그라운드 태스크명 5 /// </summary> public const string BackgroundTaskName5 = "BackgroundTask5"; /// <summary> /// 백그라운드 태스크 진행 5 /// </summary> public static string BackgroundTaskProgress5 = string.Empty; /// <summary> /// 백그라운드 태스크 등록 여부 5 /// </summary> public static bool BackgroundTaskRegistered5 = false; /// <summary> /// 백그라운드 태스크 결과 5 /// </summary> public static string BackgroundTaskResult5 = string.Empty; /// <summary> /// 백그라운드 태스크 진입점 6 /// </summary> public const string BackgroundTaskEntryPoint6 = "TestLibrary.SampleBackgroundTask1"; /// <summary> /// 백그라운드 태스크명 6 /// </summary> public const string BackgroundTaskName6 = "BackgroundTask6"; /// <summary> /// 백그라운드 태스크 그룹 ID 6 /// </summary> public const string BackgroundTaskGroupID6 = "3F2504E0-5F89-41D3-9A0C-0405E82C3333"; /// <summary> /// 백그라운드 태스크 그룹명 6 /// </summary> public const string BackgroundTaskGroupName6 = "BackgroundTaskGroup6"; /// <summary> /// 백그라운드 태스크 진행 6 /// </summary> public static string BackgroundTaskProgress6 = string.Empty; /// <summary> /// 백그라운드 태스크 등록 여부 6 /// </summary> public static bool BackgroundTaskRegistered6 = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 백그라운드 태스크 상태 구하기 - GetBackgroundTaskStatus(name) /// <summary> /// 백그라운드 태스크 상태 구하기 /// </summary> /// <param name="name">명칭</param> /// <returns>백그라운드 태스크 상태</returns> public static string GetBackgroundTaskStatus(string name) { bool registered = false; switch(name) { case BackgroundTaskName1 : registered = BackgroundTaskRegistered1; break; case BackgroundTaskName2 : registered = BackgroundTaskRegistered2; break; case BackgroundTaskName3 : registered = BackgroundTaskRegistered3; break; case BackgroundTaskName4 : registered = BackgroundTaskRegistered4; break; case BackgroundTaskName5 : registered = BackgroundTaskRegistered5; break; case BackgroundTaskName6 : registered = BackgroundTaskRegistered6; break; } string status = registered ? "등록" : "미등록"; object taskStatus; ApplicationDataContainer container = ApplicationData.Current.LocalSettings; if(container.Values.TryGetValue(name, out taskStatus)) { status += " - " + taskStatus.ToString(); } return status; } #endregion #region 백그라운드 태스크 등록 상태 업데이트하기 - UpdateBackgroundTaskRegistrationStatus(name, registered) /// <summary> /// 백그라운드 태스크 등록 상태 업데이트하기 /// </summary> /// <param name="name">명칭</param> /// <param name="registered">등록 여부</param> public static void UpdateBackgroundTaskRegistrationStatus(string name, bool registered) { switch(name) { case BackgroundTaskName1 : BackgroundTaskRegistered1 = registered; break; case BackgroundTaskName2 : BackgroundTaskRegistered2 = registered; break; case BackgroundTaskName3 : BackgroundTaskRegistered3 = registered; break; case BackgroundTaskName4 : BackgroundTaskRegistered4 = registered; break; case BackgroundTaskName5 : BackgroundTaskRegistered5 = registered; break; case BackgroundTaskName6 : BackgroundTaskRegistered6 = registered; break; } } #endregion #region 백그라운드 액세스 필요 여부 구하기 - RequiresBackgroundAccess(name) /// <summary> /// 백그라운드 액세스 필요 여부 구하기 /// </summary> /// <param name="name">명칭</param> /// <returns>백그라운드 액세스 필요 여부</returns> public static bool RequiresBackgroundAccess(string name) { if((name == BackgroundTaskName4) || (name == BackgroundTaskName5)) { return true; } else { return false; } } #endregion #region 백그라운드 태스크 등록하기 - RegisterBackgroundTask(taskEntryPoint, name, trigger, condition, taskGroup) /// <summary> /// 백그라운드 태스크 등록하기 /// </summary> /// <param name="taskEntryPoint">태스크 진입점</param> /// <param name="name">명칭</param> /// <param name="trigger">백그라운드 트리거</param> /// <param name="condition">백그라운드 조건</param> /// <param name="taskGroup">백그라운드 태스크 등록 그룹</param> /// <returns>백그라운드 태스크 등록</returns> public static BackgroundTaskRegistration RegisterBackgroundTask ( string taskEntryPoint, string name, IBackgroundTrigger trigger, IBackgroundCondition condition, BackgroundTaskRegistrationGroup taskGroup = null ) { if(RequiresBackgroundAccess(name)) { IAsyncOperation<BackgroundAccessStatus> asyncOperation = BackgroundExecutionManager.RequestAccessAsync(); } BackgroundTaskBuilder builder = new BackgroundTaskBuilder(); builder.Name = name; builder.TaskEntryPoint = taskEntryPoint; builder.SetTrigger(trigger); if(condition != null) { builder.AddCondition(condition); builder.CancelOnConditionLoss = true; // 백그라운드 작업이 실행되는 동안 조건이 변경되면 취소된다. } if(taskGroup != null) { builder.TaskGroup = taskGroup; } BackgroundTaskRegistration backgroundTaskRegistration = builder.Register(); UpdateBackgroundTaskRegistrationStatus(name, true); // 이전 완료 상태를 제거합니다. ApplicationDataContainer container = ApplicationData.Current.LocalSettings; container.Values.Remove(name); return backgroundTaskRegistration; } #endregion #region 백그라운드 태스크 등록취소하기 - UnregisterBackgroundTasks(name, taskGroup) /// <summary> /// 백그라운드 태스크 등록취소하기 /// </summary> /// <param name="name">명칭</param> /// <param name="taskGroup">백그라운드 태스크 등록 그룹</param> public static void UnregisterBackgroundTasks(string name, BackgroundTaskRegistrationGroup taskGroup = null) { // 지정된 작업 그룹이 등록된 경우 해당 그룹과 관련된 모든 백그라운드 작업을 반복하고 // 지정된 이름을 가진 모든 작업을 등록 취소합니다. if(taskGroup != null) { foreach(KeyValuePair<Guid, BackgroundTaskRegistration> keyValuePair in taskGroup.AllTasks) { if(keyValuePair.Value.Name == name) { keyValuePair.Value.Unregister(true); } } } else { // 그룹화되지 않은 모든 백그라운드 작업을 반복하고 지정된 이름으로 등록을 취소합니다. foreach(KeyValuePair<Guid, IBackgroundTaskRegistration> keyValuePair in BackgroundTaskRegistration.AllTasks) { if(keyValuePair.Value.Name == name) { keyValuePair.Value.Unregister(true); } } } UpdateBackgroundTaskRegistrationStatus(name, false); } #endregion #region 백그라운드 태스크 등록 그룹 구하기 - GetTaskGroup(id, groupName) /// <summary> /// 백그라운드 태스크 등록 그룹 구하기 /// </summary> /// <param name="id">ID</param> /// <param name="groupName">그룹명</param> /// <returns>백그라운드 태스크 등록 그룹</returns> public static BackgroundTaskRegistrationGroup GetTaskGroup(string id, string groupName) { BackgroundTaskRegistrationGroup group = BackgroundTaskRegistration.GetTaskGroup(id); if(group == null) { group = new BackgroundTaskRegistrationGroup(id, groupName); } return group; } #endregion } } |
▶ SamplePage1.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 |
<Page x:Class="TestProject.SamplePage1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid Margin="10 20 10 10"> <ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto"> <StackPanel> <TextBlock Style="{StaticResource SampleHeaderTextStyle}" Text="설명 : " /> <TextBlock Style="{StaticResource ScenarioDescriptionTextStyle}" TextWrapping="Wrap"> 시간대 변경 시스템 이벤트에 대한 백그라운드 작업을 등록합니다. 백그라운드 작업은 시간대가 변경될 때마다 실행됩니다. </TextBlock> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0 10 0 10" Orientation="Horizontal"> <Button Name="registerButton" Margin="0 0 10 0" Content="등록" Click="registerButton_Click" /> <Button Name="unregisterButton" Content="등록 취소" Click="unregisterButton_Click" /> </StackPanel> <StackPanel> <TextBlock Name="statusTextBlock" Style="{StaticResource BasicTextStyle}" TextWrapping="Wrap" Text="미등록" /> <TextBlock Name="progressTextBlock" Style="{StaticResource BasicTextStyle}" TextWrapping="Wrap" Text="" /> </StackPanel> </StackPanel> </ScrollViewer> </Grid> </Grid> </Page> |
▶ SamplePage1.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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
using System; using System.Collections.Generic; using Windows.ApplicationModel.Background; using Windows.Foundation; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 샘플 페이지 1 /// </summary> public sealed partial class SamplePage1 : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SamplePage1() /// <summary> /// 생성자 /// </summary> public SamplePage1() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnNavigatedTo(NavigationEventArgs e) { foreach(KeyValuePair<Guid, IBackgroundTaskRegistration> keyValuePair in BackgroundTaskRegistration.AllTasks) { if(keyValuePair.Value.Name == BackgroundTaskHelper.BackgroundTaskName1) { AttachEventHandler(keyValuePair.Value); BackgroundTaskHelper.UpdateBackgroundTaskRegistrationStatus(BackgroundTaskHelper.BackgroundTaskName1, true); break; } } UpdateUI(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 등록 버튼 클릭시 처리하기 - registerButton_Click(sender, e) /// <summary> /// 등록 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void registerButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskRegistration backgroundTaskRegistration = BackgroundTaskHelper.RegisterBackgroundTask ( BackgroundTaskHelper.BackgroundTaskEntryPoint1, BackgroundTaskHelper.BackgroundTaskName1, new SystemTrigger(SystemTriggerType.TimeZoneChange, false), null ); AttachEventHandler(backgroundTaskRegistration); UpdateUI(); } #endregion #region 등록 취소 버튼 클릭시 처리하기 - unregisterButton_Click(sender, e) /// <summary> /// 등록 취소 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void unregisterButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskHelper.UnregisterBackgroundTasks(BackgroundTaskHelper.BackgroundTaskName1); UpdateUI(); } #endregion #region 백그라운드 태스크 등록 진행시 처리하기 - backgroundTaskRegistration_Progress(sender, e) /// <summary> /// 백그라운드 태스크 등록 진행시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Progress(IBackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs e) { IAsyncAction action = Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { string message = $"진행률 : {e.Progress}%"; BackgroundTaskHelper.BackgroundTaskProgress1 = message; UpdateUI(); } ); } #endregion #region 백그라운드 태스크 등록 완료시 처리하기 - backgroundTaskRegistration_Completed(sender, e) /// <summary> /// 백그라운드 태스크 등록 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Completed(IBackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs e) { UpdateUI(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 이벤트 핸들러 부착하기 - AttachEventHandler(backgroundTaskRegistration) /// <summary> /// 이벤트 핸들러 부착하기 /// </summary> /// <param name="backgroundTaskRegistration">백그라운드 태스크 등록</param> private void AttachEventHandler(IBackgroundTaskRegistration backgroundTaskRegistration) { backgroundTaskRegistration.Progress += backgroundTaskRegistration_Progress; backgroundTaskRegistration.Completed += backgroundTaskRegistration_Completed; } #endregion #region UI 업데이트하기 - UpdateUI() /// <summary> /// UI 업데이트하기 /// </summary> private async void UpdateUI() { await Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { this.registerButton.IsEnabled = !BackgroundTaskHelper.BackgroundTaskRegistered1; this.unregisterButton.IsEnabled = BackgroundTaskHelper.BackgroundTaskRegistered1; this.statusTextBlock.Text = BackgroundTaskHelper.GetBackgroundTaskStatus(BackgroundTaskHelper.BackgroundTaskName1); this.progressTextBlock.Text = BackgroundTaskHelper.BackgroundTaskProgress1; } ); } #endregion } } |
▶ SamplePage2.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 |
<Page x:Class="TestProject.SamplePage2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid Margin="10 20 10 10" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto"> <StackPanel> <TextBlock Style="{StaticResource SampleHeaderTextStyle}" Text="설명 : " /> <TextBlock Style="{StaticResource ScenarioDescriptionTextStyle}" TextWrapping="Wrap"> 시간대 변경 시스템 이벤트 및 인터넷 사용 가능 시스템 조건에 대한 백그라운드 작업을 등록합니다. 사용 가능한 인터넷 연결이 있는 경우 표준 시간대가 변경될 때마다 백그라운드 작업이 실행됩니다. 백그라운드 작업이 실행되는 동안 인터넷 연결이 끊어지면 작업이 취소됩니다. </TextBlock> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0 10 0 10" Orientation="Horizontal"> <Button Name="registerButton" Margin="0 0 10 0" Content="등록" Click="registerButton_Click" /> <Button Name="unregisterButton" Content="등록 취소" Click="unregisterButton_Click" /> </StackPanel> <StackPanel> <TextBlock Name="statusTextBlock" Style="{StaticResource BasicTextStyle}" TextWrapping="Wrap" Text="미등록" /> <TextBlock Name="progressTextBlock" Style="{StaticResource BasicTextStyle}" TextWrapping="Wrap" Text="" /> </StackPanel> </StackPanel> </ScrollViewer> </Grid> </Grid> </Page> |
▶ SamplePage2.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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
using System; using System.Collections.Generic; using Windows.ApplicationModel.Background; using Windows.Foundation; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 샘플 페이지 2 /// </summary> public sealed partial class SamplePage2 : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SamplePage2() /// <summary> /// 생성자 /// </summary> public SamplePage2() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnNavigatedTo(NavigationEventArgs e) { foreach(KeyValuePair<Guid, IBackgroundTaskRegistration> keyValuePair in BackgroundTaskRegistration.AllTasks) { if(keyValuePair.Value.Name == BackgroundTaskHelper.BackgroundTaskName2) { AttachEventHandler(keyValuePair.Value); BackgroundTaskHelper.UpdateBackgroundTaskRegistrationStatus(BackgroundTaskHelper.BackgroundTaskName2, true); break; } } UpdateUI(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 등록 버튼 클릭시 처리하기 - registerButton_Click(sender, e) /// <summary> /// 등록 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void registerButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskRegistration backgroundTaskRegistration = BackgroundTaskHelper.RegisterBackgroundTask ( BackgroundTaskHelper.BackgroundTaskEntryPoint2, BackgroundTaskHelper.BackgroundTaskName2, new SystemTrigger(SystemTriggerType.TimeZoneChange, false), new SystemCondition(SystemConditionType.InternetAvailable) ); AttachEventHandler(backgroundTaskRegistration); UpdateUI(); } #endregion #region 등록 취소 버튼 클릭시 처리하기 - unregisterButton_Click(sender, e) /// <summary> /// 등록 취소 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void unregisterButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskHelper.UnregisterBackgroundTasks(BackgroundTaskHelper.BackgroundTaskName2); UpdateUI(); } #endregion #region 백그라운드 태스크 등록 진행시 처리하기 - backgroundTaskRegistration_Progress(sender, e) /// <summary> /// 백그라운드 태스크 등록 진행시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Progress(IBackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs e) { IAsyncAction action = Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { string message = $"진행률 : {e.Progress}%"; BackgroundTaskHelper.BackgroundTaskProgress2 = message; UpdateUI(); } ); } #endregion #region 백그라운드 태스크 등록 완료시 처리하기 - backgroundTaskRegistration_Completed(sender, e) /// <summary> /// 백그라운드 태스크 등록 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Completed(IBackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs e) { UpdateUI(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 이벤트 핸들러 부착하기 - AttachEventHandler(backgroundTaskRegistration) /// <summary> /// 이벤트 핸들러 부착하기 /// </summary> /// <param name="backgroundTaskRegistration">백그라운드 태스크 등록</param> private void AttachEventHandler(IBackgroundTaskRegistration backgroundTaskRegistration) { backgroundTaskRegistration.Progress += backgroundTaskRegistration_Progress; backgroundTaskRegistration.Completed += backgroundTaskRegistration_Completed; } #endregion #region UI 업데이트하기 - UpdateUI() /// <summary> /// UI 업데이트하기 /// </summary> private async void UpdateUI() { await Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { this.registerButton.IsEnabled = !BackgroundTaskHelper.BackgroundTaskRegistered2; this.unregisterButton.IsEnabled = BackgroundTaskHelper.BackgroundTaskRegistered2; this.statusTextBlock.Text = BackgroundTaskHelper.GetBackgroundTaskStatus(BackgroundTaskHelper.BackgroundTaskName2); this.progressTextBlock.Text = BackgroundTaskHelper.BackgroundTaskProgress2; } ); } #endregion } } |
▶ SamplePage3.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 |
<Page x:Class="TestProject.SamplePage3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid Margin="10 20 10 10" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto"> <StackPanel> <TextBlock Style="{StaticResource SampleHeaderTextStyle}" Text="설명 : " /> <TextBlock Style="{StaticResource ScenarioDescriptionTextStyle}" TextWrapping="Wrap"> 서비스 완료 이벤트에 대한 백그라운드 작업을 등록합니다. 백그라운드 작업은 애플리케이션이 최신 버전으로 업데이트될 때 실행됩니다. </TextBlock> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,10,0,10" Orientation="Horizontal"> <Button Name="registerButton" Margin="0 0 10 0" Content="등록" Click="registerButton_Click" /> <Button Name="unregisterButton" Content="미등록" Click="unregisterButton_Click" /> </StackPanel> <StackPanel> <TextBlock Name="statusTextBlock" Style="{StaticResource BasicTextStyle}" Text="Unregistered" /> <TextBlock Name="progressTextBlock" Style="{StaticResource BasicTextStyle}" Text="" /> </StackPanel> </StackPanel> </ScrollViewer> </Grid> </Grid> </Page> |
▶ SamplePage3.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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
using System; using System.Collections.Generic; using Windows.ApplicationModel.Background; using Windows.Foundation; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 샘플 페이지 3 /// </summary> public sealed partial class SamplePage3 : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SamplePage3() /// <summary> /// 생성자 /// </summary> public SamplePage3() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnNavigatedTo(NavigationEventArgs e) { foreach(KeyValuePair<Guid, IBackgroundTaskRegistration> keyValuePair in BackgroundTaskRegistration.AllTasks) { if(keyValuePair.Value.Name == BackgroundTaskHelper.BackgroundTaskName3) { AttachEventHandler(keyValuePair.Value); BackgroundTaskHelper.UpdateBackgroundTaskRegistrationStatus(BackgroundTaskHelper.BackgroundTaskName3, true); break; } } UpdateUI(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 등록 버튼 클릭시 처리하기 - registerButton_Click(sender, e) /// <summary> /// 등록 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void registerButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskRegistration backgroundTaskRegistration = BackgroundTaskHelper.RegisterBackgroundTask ( BackgroundTaskHelper.BackgroundTaskEntryPoint3, BackgroundTaskHelper.BackgroundTaskName3, new SystemTrigger(SystemTriggerType.ServicingComplete, false), null ); AttachEventHandler(backgroundTaskRegistration); UpdateUI(); } #endregion #region 등록 취소 버튼 클릭시 처리하기 - unregisterButton_Click(sender, e) /// <summary> /// 등록 취소 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void unregisterButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskHelper.UnregisterBackgroundTasks(BackgroundTaskHelper.BackgroundTaskName3); UpdateUI(); } #endregion #region 백그라운드 태스크 등록 진행시 처리하기 - backgroundTaskRegistration_Progress(sender, e) /// <summary> /// 백그라운드 태스크 등록 진행시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Progress(IBackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs e) { IAsyncAction action = Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { string message = $"진행률 : {e.Progress}%"; BackgroundTaskHelper.BackgroundTaskProgress3 = message; UpdateUI(); } ); } #endregion #region 백그라운드 태스크 등록 완료시 처리하기 - backgroundTaskRegistration_Completed(sender, e) /// <summary> /// 백그라운드 태스크 등록 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Completed(IBackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs e) { UpdateUI(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 이벤트 핸들러 부착하기 - AttachEventHandler(backgroundTaskRegistration) /// <summary> /// 이벤트 핸들러 부착하기 /// </summary> /// <param name="backgroundTaskRegistration">백그라운드 태스크 등록</param> private void AttachEventHandler(IBackgroundTaskRegistration backgroundTaskRegistration) { backgroundTaskRegistration.Progress += backgroundTaskRegistration_Progress; backgroundTaskRegistration.Completed += backgroundTaskRegistration_Completed; } #endregion #region UI 업데이트하기 - UpdateUI() /// <summary> /// UI 업데이트하기 /// </summary> private async void UpdateUI() { await Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { this.registerButton.IsEnabled = !BackgroundTaskHelper.BackgroundTaskRegistered3; this.unregisterButton.IsEnabled = BackgroundTaskHelper.BackgroundTaskRegistered3; this.progressTextBlock.Text = BackgroundTaskHelper.BackgroundTaskProgress3; this.statusTextBlock.Text = BackgroundTaskHelper.GetBackgroundTaskStatus(BackgroundTaskHelper.BackgroundTaskName3); } ); } #endregion } } |
▶ SamplePage4.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 |
<Page x:Class="TestProject.SamplePage4" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid Margin="10 20 10 10" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto"> <StackPanel> <TextBlock Style="{StaticResource SampleHeaderTextStyle}" Text="설명 : " /> <TextBlock Style="{StaticResource ScenarioDescriptionTextStyle}" TextWrapping="Wrap"> 시간 트리거 이벤트에 대한 백그라운드 작업을 등록하고 잠금 화면에 애플리케이션을 추가합니다. 백그라운드 작업은 앱이 잠금 화면에 있는 경우 15분마다 실행됩니다. </TextBlock> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0 10 0 10" Orientation="Horizontal"> <Button Name="registerButton" Margin="0 0 10 0" Content="등록" Click="registerButton_Click" /> <Button Name="unregisterButton" Content="미등록" Click="unregisterButton_Click" /> </StackPanel> <StackPanel> <TextBlock Name="statusTextBlock" Style="{StaticResource BasicTextStyle}" Text="Unregistered" /> <TextBlock Name="progressTextBlock" Style="{StaticResource BasicTextStyle}" Text="" /> </StackPanel> </StackPanel> </ScrollViewer> </Grid> </Grid> </Page> |
▶ SamplePage4.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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
using System; using System.Collections.Generic; using Windows.ApplicationModel.Background; using Windows.Foundation; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 샘플 페이지 4 /// </summary> public sealed partial class SamplePage4 : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SamplePage4() /// <summary> /// 생성자 /// </summary> public SamplePage4() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnNavigatedTo(NavigationEventArgs e) { foreach(KeyValuePair<Guid, IBackgroundTaskRegistration> keyValuePair in BackgroundTaskRegistration.AllTasks) { if(keyValuePair.Value.Name == BackgroundTaskHelper.BackgroundTaskName4) { AttachEventHandler(keyValuePair.Value); BackgroundTaskHelper.UpdateBackgroundTaskRegistrationStatus(BackgroundTaskHelper.BackgroundTaskName4, true); break; } } UpdateUI(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 등록 버튼 클릭시 처리하기 - registerButton_Click(sender, e) /// <summary> /// 등록 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void registerButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskRegistration backgroundTaskRegistration = BackgroundTaskHelper.RegisterBackgroundTask ( BackgroundTaskHelper.BackgroundTaskEntryPoint4, BackgroundTaskHelper.BackgroundTaskName4, new TimeTrigger(15, false), // 15분 미만으로 등록하면 예외가 발생한다. null ); AttachEventHandler(backgroundTaskRegistration); UpdateUI(); } #endregion #region 등록 취소 버튼 클릭시 처리하기 - unregisterButton_Click(sender, e) /// <summary> /// 등록 취소 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void unregisterButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskHelper.UnregisterBackgroundTasks(BackgroundTaskHelper.BackgroundTaskName4); UpdateUI(); } #endregion #region 백그라운드 태스크 등록 진행시 처리하기 - backgroundTaskRegistration_Progress(sender, e) /// <summary> /// 백그라운드 태스크 등록 진행시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Progress(IBackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs e) { IAsyncAction action = Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { string message = $"진행률 : {e.Progress}%"; BackgroundTaskHelper.BackgroundTaskProgress4 = message; UpdateUI(); } ); } #endregion #region 백그라운드 태스크 등록 완료시 처리하기 - backgroundTaskRegistration_Completed(sender, e) /// <summary> /// 백그라운드 태스크 등록 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Completed(IBackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs e) { UpdateUI(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 이벤트 핸들러 부착하기 - AttachEventHandler(backgroundTaskRegistration) /// <summary> /// 이벤트 핸들러 부착하기 /// </summary> /// <param name="backgroundTaskRegistration">백그라운드 태스크 등록</param> private void AttachEventHandler(IBackgroundTaskRegistration backgroundTaskRegistration) { backgroundTaskRegistration.Progress += backgroundTaskRegistration_Progress; backgroundTaskRegistration.Completed += backgroundTaskRegistration_Completed; } #endregion #region UI 업데이트하기 - UpdateUI() /// <summary> /// UI 업데이트하기 /// </summary> private async void UpdateUI() { await Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { this.registerButton.IsEnabled = !BackgroundTaskHelper.BackgroundTaskRegistered4; this.unregisterButton.IsEnabled = BackgroundTaskHelper.BackgroundTaskRegistered4; this.progressTextBlock.Text = BackgroundTaskHelper.BackgroundTaskProgress4; this.statusTextBlock.Text = BackgroundTaskHelper.GetBackgroundTaskStatus(BackgroundTaskHelper.BackgroundTaskName4); } ); } #endregion } } |
▶ SamplePage5.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 |
<Page x:Class="TestProject.SamplePage5" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="10 20 10 10"> <ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto"> <StackPanel> <TextBlock Style="{StaticResource SampleHeaderTextStyle}" Text="설명 : " /> <TextBlock Style="{StaticResource ScenarioDescriptionTextStyle}" TextWrapping="Wrap"> 애플리케이션 트리거 이벤트에 대한 백그라운드 작업을 등록하고 백그라운드 액세스를 요청한 다음 이벤트에 신호를 보냅니다. </TextBlock> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0 10 0 10" Orientation="Horizontal"> <Button Name="registerButton" Content="등록" Click="registerButton_Click" /> <Button Name="unregisterButton" Margin="10 0 10 0" Content="등록 취소" Click="unregisterButton_Click" /> <Button Name="signalButton" Content="신호" Click="signalButton_Click" /> </StackPanel> <StackPanel> <TextBlock Name="statusTextBlock" Style="{StaticResource BasicTextStyle}" Text="미등록" /> <TextBlock Name="progressTextBlock" Style="{StaticResource BasicTextStyle}" Text="" /> <TextBlock Name="resultTextBlock" Style="{StaticResource BasicTextStyle}" Text="" /> </StackPanel> </StackPanel> </ScrollViewer> </Grid> </Grid> </Page> |
▶ SamplePage5.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 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 |
using System; using System.Collections.Generic; using Windows.ApplicationModel.Background; using Windows.Foundation; using Windows.Storage; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 샘플 페이지 5 /// </summary> public sealed partial class SamplePage5 : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 애플리케이션 트리거 /// </summary> private ApplicationTrigger trigger = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SamplePage5() /// <summary> /// 생성자 /// </summary> public SamplePage5() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnNavigatedTo(NavigationEventArgs e) { foreach(KeyValuePair<Guid, IBackgroundTaskRegistration> keyValuePair in BackgroundTaskRegistration.AllTasks) { if(keyValuePair.Value.Name == BackgroundTaskHelper.BackgroundTaskName5) { AttachEventHandler(keyValuePair.Value); BackgroundTaskHelper.UpdateBackgroundTaskRegistrationStatus(BackgroundTaskHelper.BackgroundTaskName5, true); break; } } this.trigger = new ApplicationTrigger(); UpdateUI(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 등록 버튼 클릭시 처리하기 - registerButton_Click(sender, e) /// <summary> /// 등록 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void registerButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskRegistration backgroundTaskRegistration = BackgroundTaskHelper.RegisterBackgroundTask ( BackgroundTaskHelper.BackgroundTaskEntryPoint5, BackgroundTaskHelper.BackgroundTaskName5, this.trigger, null ); AttachEventHandler(backgroundTaskRegistration); UpdateUI(); } #endregion #region 등록 취소 버튼 클릭시 처리하기 - unregisterButton_Click(sender, e) /// <summary> /// 등록 취소 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void unregisterButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskHelper.UnregisterBackgroundTasks(BackgroundTaskHelper.BackgroundTaskName5); BackgroundTaskHelper.BackgroundTaskResult5 = string.Empty; UpdateUI(); } #endregion #region 신호 버튼 클릭시 처리하기 - signalButton_Click(sender, e) /// <summary> /// 신호 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void signalButton_Click(object sender, RoutedEventArgs e) { ApplicationDataContainer container = ApplicationData.Current.LocalSettings; container.Values.Remove(BackgroundTaskHelper.BackgroundTaskName5); ApplicationTriggerResult result = await this.trigger.RequestAsync(); BackgroundTaskHelper.BackgroundTaskResult5 = $"신호 결과 : {result.ToString()}"; UpdateUI(); } #endregion #region 백그라운드 태스크 등록 진행시 처리하기 - backgroundTaskRegistration_Progress(sender, e) /// <summary> /// 백그라운드 태스크 등록 진행시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Progress(IBackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs e) { IAsyncAction action = Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { string message = $"진행률 : {e.Progress}%"; BackgroundTaskHelper.BackgroundTaskProgress5 = message; UpdateUI(); } ); } #endregion #region 백그라운드 태스크 등록 완료시 처리하기 - backgroundTaskRegistration_Completed(sender, e) /// <summary> /// 백그라운드 태스크 등록 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Completed(IBackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs e) { UpdateUI(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 이벤트 핸들러 부착하기 - AttachEventHandler(backgroundTaskRegistration) /// <summary> /// 이벤트 핸들러 부착하기 /// </summary> /// <param name="backgroundTaskRegistration">백그라운드 태스크 등록</param> private void AttachEventHandler(IBackgroundTaskRegistration backgroundTaskRegistration) { backgroundTaskRegistration.Progress += backgroundTaskRegistration_Progress; backgroundTaskRegistration.Completed += backgroundTaskRegistration_Completed; } #endregion #region UI 업데이트하기 - UpdateUI() /// <summary> /// UI 업데이트하기 /// </summary> private async void UpdateUI() { await Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { this.registerButton.IsEnabled = !BackgroundTaskHelper.BackgroundTaskRegistered5; this.unregisterButton.IsEnabled = BackgroundTaskHelper.BackgroundTaskRegistered5; this.signalButton.IsEnabled = BackgroundTaskHelper.BackgroundTaskRegistered5 & (this.trigger != null); this.progressTextBlock.Text = BackgroundTaskHelper.BackgroundTaskProgress5; this.statusTextBlock.Text = BackgroundTaskHelper.GetBackgroundTaskStatus(BackgroundTaskHelper.BackgroundTaskName5); this.resultTextBlock.Text = BackgroundTaskHelper.BackgroundTaskResult5; } ); } #endregion } } |
▶ SamplePage6.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 |
<Page x:Class="TestProject.SamplePage6" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid Margin="10 20 10 10 " Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto"> <StackPanel> <TextBlock Style="{StaticResource SampleHeaderTextStyle}" Text="설명 : " /> <TextBlock Style="{StaticResource ScenarioDescriptionTextStyle}" TextWrapping="Wrap"> 표준 시간대 변경 트리거 이벤트에 대한 백그라운드 작업을 백그라운드 작업 등록 그룹에 등록합니다. 그룹에 등록하면 그룹 해제된 모든 작업이 등록 해제될 때 작업 등록이 활성 상태로 유지됩니다. </TextBlock> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0 10 0 10" Orientation="Horizontal"> <Button Name="registerButton" Content="그룹 태스크 등록" Click="registerButton_Click" /> <Button Name="unregisterButton" Margin="10 0 10 0" Content="그룹 태스크 등록 취소" Click="unregisterButton_Click" /> </StackPanel> <Button Name="unregisterUngroupedTaskButton" Margin="0 0 0 10" Click="unregisterUngroupedTaskButton_Click"> <TextBlock TextWrapping="Wrap" Text="일반 태스크 등록 취소" /> </Button> <StackPanel> <TextBlock Name="statusTextBlock" Style="{StaticResource BasicTextStyle}" Text="미등록" /> <TextBlock Name="progressTextBlock" Style="{StaticResource BasicTextStyle}" Text="" /> <TextBlock Name="Result" Style="{StaticResource BasicTextStyle}" Text="" /> </StackPanel> </StackPanel> </ScrollViewer> </Grid> </Grid> </Page> |
▶ SamplePage6.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 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 |
using System; using System.Collections.Generic; using Windows.ApplicationModel.Background; using Windows.Foundation; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 샘플 페이지 6 /// </summary> public sealed partial class SamplePage6 : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 백그라운드 태스크 등록 그룹 /// </summary> private BackgroundTaskRegistrationGroup group; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SamplePage6() /// <summary> /// 생성자 /// </summary> public SamplePage6() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnNavigatedTo(NavigationEventArgs e) { this.group = BackgroundTaskHelper.GetTaskGroup ( BackgroundTaskHelper.BackgroundTaskGroupID6, BackgroundTaskHelper.BackgroundTaskGroupName6 ); foreach(KeyValuePair<Guid, BackgroundTaskRegistration> keyValuePair in this.group.AllTasks) { if(keyValuePair.Value.Name == BackgroundTaskHelper.BackgroundTaskName6) { AttachEventHandler(keyValuePair.Value); BackgroundTaskHelper.UpdateBackgroundTaskRegistrationStatus(BackgroundTaskHelper.BackgroundTaskName6, true); break; } } UpdateUI(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 그룹 태스크 등록 버튼 클릭시 처리하기 - registerButton_Click(sender, e) /// <summary> /// 그룹 태스크 등록 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void registerButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskRegistration backgroundTaskRegistration = BackgroundTaskHelper.RegisterBackgroundTask ( BackgroundTaskHelper.BackgroundTaskEntryPoint6, BackgroundTaskHelper.BackgroundTaskName6, new SystemTrigger(SystemTriggerType.TimeZoneChange, false), null, this.group ); AttachEventHandler(backgroundTaskRegistration); UpdateUI(); } #endregion #region 그룹 태스크 등록 취소 버튼 클릭시 처리하기 - unregisterButton_Click(sender, e) /// <summary> /// 그룹 태스크 등록 취소 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void unregisterButton_Click(object sender, RoutedEventArgs e) { BackgroundTaskHelper.UnregisterBackgroundTasks(BackgroundTaskHelper.BackgroundTaskName6, this.group); UpdateUI(); } #endregion #region 일반 태스크 등록 취소 버튼 클릭시 처리하기 - unregisterUngroupedTaskButton_Click(sender, e) /// <summary> /// 일반 태스크 등록 취소 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void unregisterUngroupedTaskButton_Click(object sender, RoutedEventArgs e) { foreach(KeyValuePair<Guid, IBackgroundTaskRegistration> keyValuePair in BackgroundTaskRegistration.AllTasks) { keyValuePair.Value.Unregister(true); BackgroundTaskHelper.UpdateBackgroundTaskRegistrationStatus(keyValuePair.Value.Name, false); } } #endregion #region 백그라운드 태스크 등록 진행시 처리하기 - backgroundTaskRegistration_Progress(sender, e) /// <summary> /// 백그라운드 태스크 등록 진행시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Progress(IBackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs e) { IAsyncAction action = Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { string message = $"진행률 : {e.Progress}%"; BackgroundTaskHelper.BackgroundTaskProgress6 = message; UpdateUI(); } ); } #endregion #region 백그라운드 태스크 등록 완료시 처리하기 - backgroundTaskRegistration_Completed(sender, e) /// <summary> /// 백그라운드 태스크 등록 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundTaskRegistration_Completed(IBackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs e) { UpdateUI(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 이벤트 핸들러 부착하기 - AttachEventHandler(backgroundTaskRegistration) /// <summary> /// 이벤트 핸들러 부착하기 /// </summary> /// <param name="backgroundTaskRegistration">백그라운드 태스크 등록</param> private void AttachEventHandler(IBackgroundTaskRegistration backgroundTaskRegistration) { backgroundTaskRegistration.Progress += backgroundTaskRegistration_Progress; backgroundTaskRegistration.Completed += backgroundTaskRegistration_Completed; } #endregion #region UI 업데이트하기 - UpdateUI() /// <summary> /// UI 업데이트하기 /// </summary> private async void UpdateUI() { await Dispatcher.RunAsync ( CoreDispatcherPriority.Normal, () => { this.registerButton.IsEnabled = !BackgroundTaskHelper.BackgroundTaskRegistered6; this.unregisterButton.IsEnabled = BackgroundTaskHelper.BackgroundTaskRegistered6; this.progressTextBlock.Text = BackgroundTaskHelper.BackgroundTaskProgress6; this.statusTextBlock.Text = BackgroundTaskHelper.GetBackgroundTaskStatus(BackgroundTaskHelper.BackgroundTaskName6); } ); } #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 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 |
<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" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal"> <Border Background="{ThemeResource SystemControlBackgroundChromeMediumBrush}"> <ToggleButton Style="{StaticResource SymbolButton}" VerticalAlignment="Center" Foreground="{ThemeResource ApplicationForegroundThemeBrush}" Click="paneToggleButton_Click"> <ToggleButton.Content> <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="" /> </ToggleButton.Content> </ToggleButton> </Border> <Image VerticalAlignment="Center" Margin="5 5 0 0" AutomationProperties.AccessibilityView="Raw" Stretch="None" Source="IMAGE/windows-sdk.png" /> <TextBlock Style="{StaticResource TagLineTextStyle}" VerticalAlignment="Center" Margin="5 0 0 0" Text="유니버셜 윈도우즈 플랫폼 예제" /> </StackPanel> <SplitView Name="splitView" Grid.Row="1" DisplayMode="Inline" IsPaneOpen="True"> <SplitView.Pane> <RelativePanel Margin="10 0 0 0"> <TextBlock Name="sampleTitle" Style="{StaticResource SampleHeaderTextStyle}" Margin="0 10 0 0" TextWrapping="Wrap" Text="백그라운드 태스크 예제" /> <ListBox Name="listBox" RelativePanel.Below="sampleTitle" RelativePanel.AlignBottomWithPanel="True" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0 10 0 0" BorderThickness="0" Background="Transparent" SelectionMode="Single" SelectionChanged="listBox_SelectionChanged"> <ListBox.ItemTemplate> <DataTemplate x:DataType="local:Scenario"> <TextBlock Text="{x:Bind Title}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </RelativePanel> </SplitView.Pane> <RelativePanel> <Frame Name="frame" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignTopWithPanel="True" RelativePanel.Above="statusStackPanel" Margin="5" /> <StackPanel Name="statusStackPanel" Orientation="Vertical" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignBottomWithPanel="True"> <TextBlock Margin="10 0 0 10" TextWrapping="Wrap" Text="상태 : " /> <Border Name="statusBorder" Margin="0 0 0 0"> <ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto" MaxHeight="200"> <TextBlock Name="statusTextBlock" AutomationProperties.LiveSetting="Assertive" Margin="10 10 10 20" MaxWidth="{Binding ElementName=Splitter, Path=ActualWidth}" TextWrapping="Wrap" FontWeight="Bold" /> </ScrollViewer> </Border> </StackPanel> </RelativePanel> </SplitView> </Grid> </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 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 |
using System; using System.Collections.Generic; using Windows.Foundation; using Windows.Graphics.Display; using Windows.UI; using Windows.UI.Core; using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Automation.Peers; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 현재 페이지 /// </summary> public static MainPage CurrentPage; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 시나리오 리스트 /// </summary> private List<Scenario> scenarioList = new List<Scenario> { new Scenario() { Title = "백그라운드 태스크" , ClassType = typeof(SamplePage1) }, new Scenario() { Title = "조건부 백그라운드 태스크" , ClassType = typeof(SamplePage2) }, new Scenario() { Title = "서비스 완료 태스크" , ClassType = typeof(SamplePage3) }, new Scenario() { Title = "시간 트리거 백그라운드 태스크" , ClassType = typeof(SamplePage4) }, new Scenario() { Title = "애플리케이션 트리거 백그라운드 태스크", ClassType = typeof(SamplePage5) }, new Scenario() { Title = "그룹 백그라운드 태스크" , ClassType = typeof(SamplePage6) } }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); #region 윈도우 크기를 설정한다. double width = 800d; double height = 600d; double dpi = (double)DisplayInformation.GetForCurrentView().LogicalDpi; ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; Size windowSize = new Size(width * 96d / dpi, height * 96d / dpi); ApplicationView.PreferredLaunchViewSize = windowSize; Window.Current.Activate(); ApplicationView.GetForCurrentView().TryResizeView(windowSize); #endregion #region 윈도우 제목을 설정한다. ApplicationView.GetForCurrentView().Title = "백그라운드 태스크 사용하기"; #endregion CurrentPage = this; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnNavigatedTo(NavigationEventArgs e) { List<Scenario> sourceList = new List<Scenario>(); int i = 1; foreach(Scenario scenario in scenarioList) { sourceList.Add ( new Scenario { Title = $"{i++}) {scenario.Title}", ClassType = scenario.ClassType } ); } this.listBox.ItemsSource = sourceList; if(Window.Current.Bounds.Width < 640) { this.listBox.SelectedIndex = -1; } else { this.listBox.SelectedIndex = 0; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 창 토글 버튼 클릭시 처리하기 - paneToggleButton_Click(sender, e) /// <summary> /// 창 토글 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void paneToggleButton_Click(object sender, RoutedEventArgs e) { this.splitView.IsPaneOpen = !splitView.IsPaneOpen; } #endregion #region 리스트 박스 선택 변경시 처리하기 - listBox_SelectionChanged(sender, e) /// <summary> /// 리스트 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ShowStatusMessage(string.Empty, NotifyType.StatusMessage); ListBox listBox = sender as ListBox; Scenario scenario = listBox.SelectedItem as Scenario; if(scenario != null) { this.frame.Navigate(scenario.ClassType); if(Window.Current.Bounds.Width < 640) { this.splitView.IsPaneOpen = false; } } } #endregion #region 상태 메시지 업데이트하기 - UpdateStatusMessage(message, notifyType) /// <summary> /// 상태 메시지 업데이트하기 /// </summary> /// <param name="message">메시지</param> /// <param name="notifyType">통지 타입</param> private void UpdateStatusMessage(string message, NotifyType notifyType) { switch(notifyType) { case NotifyType.StatusMessage : this.statusBorder.Background = new SolidColorBrush(Colors.Green); break; case NotifyType.ErrorMessage : this.statusBorder.Background = new SolidColorBrush(Colors.Red); break; } this.statusTextBlock.Text = message; this.statusBorder.Visibility = (this.statusTextBlock.Text != string.Empty) ? Visibility.Visible : Visibility.Collapsed; if(this.statusTextBlock.Text != string.Empty) { this.statusBorder.Visibility = Visibility.Visible; this.statusStackPanel.Visibility = Visibility.Visible; } else { this.statusBorder.Visibility = Visibility.Collapsed; this.statusStackPanel.Visibility = Visibility.Collapsed; } AutomationPeer peer = FrameworkElementAutomationPeer.FromElement(this.statusTextBlock); if(peer != null) { peer.RaiseAutomationEvent(AutomationEvents.LiveRegionChanged); } } #endregion #region 상태 메시지 표시하기 - ShowStatusMessage(message, notifyType) /// <summary> /// 상태 메시지 표시하기 /// </summary> /// <param name="message">메시지</param> /// <param name="notifyType">통지 타입</param> public async void ShowStatusMessage(string message, NotifyType notifyType) { if(Dispatcher.HasThreadAccess) { UpdateStatusMessage(message, notifyType); } else { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => UpdateStatusMessage(message, notifyType)); } } #endregion } } |
▶ App.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Application x:Class="TestProject.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" RequestedTheme="Dark"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/STYLE/Style.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> |
▶ App.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 |
using System; using System.Diagnostics; using Windows.ApplicationModel.Activation; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 앱 /// </summary> sealed partial class App : Application { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - App() /// <summary> /// 생성자 /// </summary> public App() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 시작시 처리하기 - OnLaunched(e) /// <summary> /// 시작시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnLaunched(LaunchActivatedEventArgs e) { #if DEBUG if(Debugger.IsAttached) { DebugSettings.EnableFrameRateCounter = false; } #endif Frame rootFrame = Window.Current.Content as Frame; if(rootFrame == null) { rootFrame = new Frame(); rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0]; rootFrame.NavigationFailed += rootFrame_NavigationFailed; if(e.PreviousExecutionState == ApplicationExecutionState.Terminated) { } Window.Current.Content = rootFrame; } if(rootFrame.Content == null) { rootFrame.Navigate(typeof(MainPage), e.Arguments); } Window.Current.Activate(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 루트 프레임 네비게이션 실패시 처리하기 - rootFrame_NavigationFailed(sender, e) /// <summary> /// 루트 프레임 네비게이션 실패시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void rootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) { throw new Exception($"페이지 로드를 실패했습니다 : {e.SourcePageType.FullName}"); } #endregion } } |