SaveAppointmentTask for Beginners in Depth

Hello, today we will learn something that is mostly needed by developers. Windows Phone 8 now allows the developer to use SaveAppointmentTask to help the user. SaveAppointmentTask saves the appointment to the calendar, whereas the task API allows you to populate many of the fields of the new appointment. Use the easy procedure provided here to use very task in your project.

Note:

The example mentioned below is explained using a new project whereas it can easily be embedded in an older project.

Open Visual Studio and create a new Windows Phone 8 project. By using solution explorer open “MainPage.xaml”.

Drag and Drop a button from the toolbox or else create it in XAML code.

  1. <Button x:Name="Appointment" Margin="103,210,96,194" Content="Appointment"  
  2. Click="Appointment_Click_1" />  
Open the “MainPage.xaml.cs” file from the Solution Explorer. Whenever you use a task in Windows Phone you need to add a reference and in the cs code you must add the following:
  1. using Microsoft.Phone.Tasks;  
Create an event handler for the button you created in the XAML code.
  1. private void Appointment_Click_1(object sender, RoutedEventArgs e)  
  2. {  
  3.   
  4. }  
And make an object of SaveAppointmentTask as follows:
  1. SaveAppointmentTask saveAppointmentTask = new SaveAppointmentTask();  
Now when you have created an object of the very task, you can use this object to facilitate the user to set the appointment. This task provides many methods that are applicable to it.

A developer has liberty to set appointment status, details and end time. Developers can even use the option IsAllDayEvent. Besides this it provides other options, like location, remainder, start time and the subject of the appointment.

How can you do it in code? Let me show this to you.
  1. saveAppointmentTask.Details="Hello, this my first appointment set by using saveAppointmentTask";  
  2. saveAppointmentTask.StartTime = currentTime.AddHours(5);  
  3. saveAppointmentTask.EndTime =  
  4. currentTime.AddHours(8);  
  5. saveAppointmentTask.Subject = "Demo";  
  6. saveAppointmentTask.IsAllDayEvent = false;  
  7. saveAppointmentTask.Reminder = Reminder.FiveMinutes;  
  8. saveAppointmentTask.AppointmentStatus = Microsoft.Phone.UserData.AppointmentStatus.Busy;  
  9. saveAppointmentTask.Show();  
You have to use this code in the event handler of the button created above. I have used the date time library to set time. To set the remainder of the task you need to do something else.

Use Reminder to set respective time that you choose. You can use different appointment statuses like busy and so on. For that you need to access user data.

This is a simple way to use saveAppointmentTask. You can now run the solution, click on the appointment button you just created.

You’ll see the following,

Using this task will improve the qualities of your application and will beautify it. So why not use it now?

Tips:

Whenever you use SaveAppointmentTask never forget to use date and time and its methods to implement it properly. One can create its object and then use it in the  task. It is always better to save the current date and time in a variable of type date time so you might not to again and again type it. One you can do like this:
  1. DateTime currentTime = DateTime.Now;  
Now use it in the task. Some of the options of the appointment are not methods, so use appropriate code to use them. As above the appointment status is set by accessing the user data. And some of the options are Boolean that might be true or false with respective to the code you write.

Personal Blog: Blend expression.