-
Notifications
You must be signed in to change notification settings - Fork 8k
/
Scenario5_ApplicationTriggerTask.xaml.cs
158 lines (142 loc) · 6.3 KB
/
Scenario5_ApplicationTriggerTask.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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using SDKTemplate;
using Windows.ApplicationModel.Background;
using Windows.Storage;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace SDKTemplate
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ApplicationTriggerTask : Page
{
// A pointer back to the main page. This is needed if you want to call methods in MainPage such
// as NotifyUser()
MainPage rootPage = MainPage.Current;
// A pointer to the ApplicationTrigger so we can signal it later
ApplicationTrigger trigger = null;
public ApplicationTriggerTask()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == BackgroundTaskSample.ApplicationTriggerTaskName)
{
AttachProgressAndCompletedHandlers(task.Value);
BackgroundTaskSample.UpdateBackgroundTaskStatus(BackgroundTaskSample.ApplicationTriggerTaskName, true);
break;
}
}
trigger = new ApplicationTrigger();
UpdateUI();
}
/// <summary>
/// Register a ApplicationTriggerTask.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void RegisterBackgroundTask(object sender, RoutedEventArgs e)
{
var task = BackgroundTaskSample.RegisterBackgroundTask(BackgroundTaskSample.SampleBackgroundTaskEntryPoint,
BackgroundTaskSample.ApplicationTriggerTaskName,
trigger,
null);
await task;
AttachProgressAndCompletedHandlers(task.Result);
UpdateUI();
}
/// <summary>
/// Unregister a ApplicationTriggerTask.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UnregisterBackgroundTask(object sender, RoutedEventArgs e)
{
BackgroundTaskSample.UnregisterBackgroundTasks(BackgroundTaskSample.ApplicationTriggerTaskName);
BackgroundTaskSample.ApplicationTriggerTaskResult = "";
UpdateUI();
}
/// <summary>
/// Signal a ApplicationTriggerTask.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void SignalBackgroundTask(object sender, RoutedEventArgs e)
{
// Reset the completion status
var settings = ApplicationData.Current.LocalSettings;
settings.Values.Remove(BackgroundTaskSample.ApplicationTriggerTaskName);
//Signal the ApplicationTrigger
var result = await trigger.RequestAsync();
BackgroundTaskSample.ApplicationTriggerTaskResult = "Signal result: " + result.ToString();
UpdateUI();
}
/// <summary>
/// Attach progress and completed handers to a background task.
/// </summary>
/// <param name="task">The task to attach progress and completed handlers to.</param>
private void AttachProgressAndCompletedHandlers(IBackgroundTaskRegistration task)
{
task.Progress += new BackgroundTaskProgressEventHandler(OnProgress);
task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
}
/// <summary>
/// Handle background task progress.
/// </summary>
/// <param name="task">The task that is reporting progress.</param>
/// <param name="e">Arguments of the progress report.</param>
private void OnProgress(IBackgroundTaskRegistration task, BackgroundTaskProgressEventArgs args)
{
var progress = "Progress: " + args.Progress + "%";
BackgroundTaskSample.ApplicationTriggerTaskProgress = progress;
UpdateUI();
}
/// <summary>
/// Handle background task completion.
/// </summary>
/// <param name="task">The task that is reporting completion.</param>
/// <param name="e">Arguments of the completion report.</param>
private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
{
UpdateUI();
}
/// <summary>
/// Update the scenario UI.
/// </summary>
private async void UpdateUI()
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
RegisterButton.IsEnabled = !BackgroundTaskSample.ApplicationTriggerTaskRegistered;
UnregisterButton.IsEnabled = BackgroundTaskSample.ApplicationTriggerTaskRegistered;
SignalButton.IsEnabled = BackgroundTaskSample.ApplicationTriggerTaskRegistered & (trigger != null);
Progress.Text = BackgroundTaskSample.ApplicationTriggerTaskProgress;
Result.Text = BackgroundTaskSample.ApplicationTriggerTaskResult;
Status.Text = BackgroundTaskSample.GetBackgroundTaskStatus(BackgroundTaskSample.ApplicationTriggerTaskName);
});
}
}
}