Introduction
Alarms and reminders are some of the various Windows Phone scheduled notification options that every developer can take advantage of to make his app more responsive and useful. They inherit from ScheduledNotfication and the ScheduledActionService class. In this tutorial we will get to know what an alarm is, what a reminder is and what the differences are between an alarm and the reminder.
- Alarms:This is how the alarm looks like in Windows Phone.
Properties:
At a specified time, the alarm dialog box pops up. It consists of:
- The app name: the name of the app from which the alarm was created.
- “Alarm” is always set as a title.
- Content: set by the app or the user.
- You can manage the alarm sound from the app.
If the user clicks on snooze the phone will launch automatically the alarm again after 9 minutes.
However if he clicks on the alarm’s UI, the app that created the alarm will be launched at it’s initial page.
2. Reminders:
Properties:
- Reminders use the default reminder sound (of the device).
- You can edit both the title and the content.
- Once the user clicks on the reminder’s UI it’ll open the app or a specific page within the app.
How to add an alarm / a reminder ?
In order to implement alarms and reminders in your project create a new Windows Phone 8 Silverlight Project named “Alarms and Reminders”.
Download Windows Phone Toolkit:
If we want to make our app really useful, then we’d better user some controls that will make the app more practical and user friendly. That’s why we’re going to use the DatePicker and TimePicker controls, which are included in the Windows Phone toolkit. To do so let’s first download the Windows Phone toolkit. In the tools menu click on NuGet Package Manager and Package Manager console.
After some moments the console will appear, write the following command :
And Visual Studio will download the Windows Phone toolkit for you.
Adding Windows Phone Toolkit:
Now let’s add our controls! In the MainPage.xaml add the following xaml code at the top of the page but below the <phone:PhoneApplicationPage statement:
1 |
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" |
Now we can use the DatePicker and TimePicker controls.
So we are going to ask the user about the date and time of the notification and we will let him choose whether to use an alarm or a reminder: try to use stackpanels, textblocks , buttons, date and time pickers to make the following UI. First let’s add our textblocks to a stackpanel that has a vertical orientation also add two buttons in a stackpanel with a horizontal orientation.Then we will put both of the stackpanels in a container stackpanel.
1 2 3 4 5 6 7 8 9 10 |
<StackPanel> <StackPanel Height="300" VerticalAlignment="Top" Margin="0,20,0,0"> <TextBlock>Pick alarm / reminder Date</TextBlock> <TextBlock>Pick alar / reminder Time</TextBlock> </StackPanel> <StackPanel Orientation="Horizontal" Height="100" HorizontalAlignment="Center"> <Button Name="addAlarmButton" Click="addAlarmButton_Click" Width="200"> Add Alarm</Button> <Button Name="addReminderButton" Click="addReminderButton_Click" Width="200">Add Reminder</Button> </StackPanel> </StackPanel> |
Then we’ll add the Date and Time picker controls.
To add a DatePicker control copy and paste the following line of code :
1 |
<toolkit:DatePicker Name="alarmDatePicker"></toolkit:DatePicker> |
And this one for the TimePicker:
1 |
<toolkit:TimePicker Name="AlarmTimePicker"></toolkit:TimePicker> |
Knowing that if we don’t use a TimePicker, the phone will set it automatically to 00.00.
After we are done with making a suitable user interface, we’ll add both the alarm and the reminder using the click event handler of the addReminderButton button and the addAlarmButton button.
How to add reminders and alarms using C# code ?
In the Code behind MainPage.xaml(MainPage.xaml.cs file) add this using statement which will let us work with the ScheduledNotificationClass
1 |
using Microsoft.Phone.Scheduler; |
In the Constructor, declare two variables named alarm and reminder of type Alarm and Reminder.
1 2 3 |
Alarm alarm; Reminder reminder; // Constructor |
Adding alarms:
In the clicked event handler of the add alarm button add the following block of code
1 2 3 4 5 6 7 8 9 10 11 12 |
private void addAlarmButton_Click(object sender, RoutedEventArgs e) { alarm = new Alarm("MyAlarm"); DateTime alarmdate = new DateTime(); DateTime alarmtime = AlarmTimePicker.Value.Value; alarmdate = alarmDatePicker.Value.Value.AddHours(alarmtime.Hour).AddMinutes(alarmtime.Minute); alarm.BeginTime = alarmdate; alarm.Content = "Alarm works just fine!"; //MessageBox.Show(alarmdate.ToString()); just to test wether the alarmdate variable has //the right value that the user entered ScheduledActionService.Add(alarm); } |
As for the addReminderButton click event, add the same code that we wrote for alarm but just replace the alarm variable with the reminder variable!
Note that you can use the reminder properties to set a title and a navigation Uri if wanted!
1 2 3 |
reminder.Title = "You've got a reminder!"; reminder.NavigationUri = new Uri("/MainPage.xaml", UriKind.Relative); //If you want to open your app or a specefic page when //the user clicks on the reminder UI |
Results:
Conclusion:
In this tutorial we learned how to download the Windows Phone toolkit, how to add it to our project and how to use 2 of its controls, the DatePicker and TimePicker, as well. We also used those controls to make a small alarm and reminder app which enables the user to set a date and a time for a wanted alarm or reminder. You can download the project sample Here
November 8, 2015 at 6:41 am
Nice Site