IT tutorials
 
Mobile
 

Windows Phone 8 : Scheduled Notifications (part 2) - Alarm Registration, Alarm Sample

4/18/2014 2:28:48 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Alarm Registration

As previously stated, registering an alarm is done via the ScheduledActionService. When using the ScheduledActionService, each notification must be given a unique name, or an exception is raised when you try to add it to the service. In the following example, we avoid reregistering a notification by detecting whether a notification with the same name has already been registered. We then use the ScheduledActionService class’s Add method to register the alarm, as shown:

Alarm alarm = new Alarm(alarmName)
{
    BeginTime = DateTime.Now.AddDays(1),
    Content = "A test alarm.",
    RecurrenceType = RecurrenceInterval.None,
    Sound = new Uri("/Sounds/Alarm.wma", UriKind.Relative),
};

if (ScheduledActionService.Find(alarm.Name) != null)
{
    ScheduledActionService.Remove(alarm.Name);
}

ScheduledActionService.Add(alarm);

The BeginTime property indicates when the alarm is due to occur. RecurrenceInterval.None specifies that the alarm is not recurring, which need not be specified because it is the default value.

The Alarm.wma audio file is located in the project and has its Build Action set to Content. When the alarm occurs, the audio file is played.

The Alarm class properties are examined in greater detail in the following section, where you explore the downloadable sample code for this section.

Alarm Sample

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:

- BeginTimeA DateTime value indicating when the alarm should occur.

- NameA unique string identifier for the alarm.

- RecurrenceTypeA RecurrenceInterval enum value that specifies whether the alarm is a one-time alarm or that it should recur every day or month.

- ExpirationTimeWhen the alarm is a recurring alarm, this value indicates when the alarm is to be deemed no longer valid and removed by the OS.

- ContentA string that is presented in the alarm dialog when the alarm occurs.

- SoundWhen 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. 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 3 shows the set alarm view, which allows the user to enter the new alarm information.

Image

FIGURE 3 The set alarm view.

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.

 
Others
 
- Windows Phone 8 : Scheduled Notifications (part 1)
- Windows Phone 8 : Walking Through the Bookshop Sample Application (part 5) - Overview of the Sample Bookshop WCF Service
- Windows Phone 8 : Walking Through the Bookshop Sample Application (part 4) - Image Caching
- Windows Phone 8 : Walking Through the Bookshop Sample Application (part 3) - Design-Time Data
- Windows Phone 8 : Walking Through the Bookshop Sample Application (part 2) - Displaying the Product List
- Windows Phone 8 : Walking Through the Bookshop Sample Application (part 1)
- Windows Phone 8 : Page Navigation - Creating an Application Splash Screen
- Windows Phone 8 : Page Navigation - Page Redirection, Hardware Back Button
- Windows Phone 8 : Page Navigation - Canceling Navigation, Cross-Page Communication
- Windows Phone 8 : Page Navigation - Navigation Using the NavigationService, Handling Page Navigation
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us