The sample code for this section includes a page that allows you to enter the details of a new alarm—including its name and begin time—and to set the alarm via an application bar button. The code is located in the Alarms directory of the WPUnleashed.BackgroundAgents project, in the BackgroundAgents solution.
Within the sample, the AlarmViewModel
class contains various properties that correspond to the following properties of the Alarm
class:
BeginTime—A DateTime
value indicating when the alarm should occur.
Name—A unique string identifier for the alarm.
RecurrenceType—A RecurrenceInterval
enum value that specifies whether the alarm is a one-time alarm or that it should recur every day or month.
ExpirationTime—When the alarm is a recurring alarm, this value indicates when the alarm is to be deemed no longer valid and removed by the OS.
Content—A string that is presented in the alarm dialog when the alarm occurs.
Sound—When the alarm occurs, a sound is played. This property is a relative URI to an audio file within the assembly, allowing you to change the default sound that is played to one of your choosing.
The viewmodel contains a single ICommand
called AlarmSetCommand
, which is initialized in the viewmodel’s constructor, as shown:
public AlarmViewModel()
{
alarmSetCommand = new DelegateCommand(obj => SetAlarm());
}
When executed, the command calls the SetAlarm
method, which creates a new Alarm
using the viewmodel’s property values. The built-in ScheduledActionService
is used to register the alarm, as shown in the following excerpt:
void SetAlarm()
{
Alarm alarm = new Alarm(alarmName)
{
BeginTime = alarmBeginTime,
ExpirationTime = alarmExpirationTime,
Content = alarmContent,
RecurrenceType = alarmRecurrenceType,
Sound = alarmSound,
/* Alarm does not support setting the Title. */
// Title = alarmTitle
};
if (ScheduledActionService.Find(alarm.Name) != null)
{
ScheduledActionService.Remove(alarm.Name);
}
ScheduledActionService.Add(alarm);
MessageService.ShowMessage("alarm set");
}
The alarmSound
field of the viewmodel is set to a Content resource located in the project’s Sounds directory, as shown:
readonly Uri alarmSound = new Uri("/Sounds/Alarm.wma", UriKind.Relative);
Although nearly all of the viewmodel’s properties are used merely as values for a new Alarm, one property, RecurrenceIntervals
, allows the user to pick the recurrence type of the alarm using a Windows Phone Toolkit ListPicker
in the view. The ListPicker
is bound to the RecurrenceIntervals
property of the viewmodel.
Population of the recurrenceIntervals
backing field is done using the custom EnumUtility
class, presented in Chapter 6, “Mastering Text Elements and Fonts.” The CreateEnumValueList
method retrieves the possible enum values using reflection. See the following excerpt:
readonly IEnumerable<RecurrenceInterval> recurrenceIntervals
= EnumUtility.CreateEnumValueList<RecurrenceInterval>();
public IEnumerable<RecurrenceInterval> RecurrenceIntervals
{
get
{
return recurrenceIntervals;
}
}
By using reflection to retrieve the list of enum values, it means they do not have to be hard-coded into the viewmodel.
The view allows the user to set the properties of a new alarm. Windows Phone Toolkit TimePicker
and DatePicker
controls are used to set the beginning and expiration times of the alarm, as shown in the following excerpt:
<StackPanel Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="name" />
<TextBox Text="{Binding AlarmName, Mode=TwoWay}" />
<TextBlock Text="content" />
<TextBox Text="{Binding AlarmContent, Mode=TwoWay}" />
<StackPanel Orientation="Horizontal">
<StackPanel>
<TextBlock Text="begin time" />
<toolkit:TimePicker
Value="{Binding AlarmBeginTime, Mode=TwoWay}" />
</StackPanel>
<StackPanel>
<TextBlock Text="expires" />
<toolkit:DatePicker
Value="{Binding AlarmExpirationTime, Mode=TwoWay}" />
</StackPanel>
</StackPanel>
<toolkit:ListPicker
Header="recurrence interval"
ItemsSource="{Binding RecurrenceIntervals}"
SelectedItem="{Binding AlarmRecurrenceType, Mode=TwoWay}" />
</StackPanel>
The view also includes an AppBar
with a button that is bound to the AlarmSetCommand
, like so:
<u:AppBar>
<u:AppBarIconButton
Command="{Binding AlarmSetCommand}"
Text="Set Alarm"
IconUri="/Images/ApplicationBarIcons/Check.png" />
</u:AppBar>
The various viewmodel properties are initialized with default values. Figure 32.3 shows the set alarm view, which allows the user to enter the new alarm information.
When the set alarm button is tapped, the AlarmSetCommand
is executed, registering the alarm with the OS. When the begin time for the alarm comes around, the alarm dialog is displayed to the user.
18.191.67.40