In this article we are going to see how to programmatically access the calendar data from your windows phone application. Calendar data holds the appointments information which can be accessed by taking a reference to the Appointment object and perform an asynchronous search to get the information from the list of available appointments from the calendar. In our previous how to we have seen how to programmatically access the Camera control flash which helps in providing the capability to control from the application developed. Let us see the steps on how to achieve this task in our Windows Phone application development. Open Visual Studio 2012 IDE and create a new Windows Phone project with a valid project name as shown in the screen below. [more]
Clicking on OK will create the project and the solution with the list of default files and folders that are required to run the application. It will take some time to create these files based on your system configuration, so once everything is ready we can see the Visual Studio IDE with the project as shown in the screen below.
Let us add some controls which basically triggers the Calendar to get the appointments, since we are going to test in the emulator we will not have any appointments by default. So in order to test this code sample connect to the physical device and you can see the search which happens on the calendar and gets the appointment asynchronously.
XAML Code:
[code:c#]<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="F5debug How to Series" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Calendar Data" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!–ContentPanel – place additional content here–>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Get Appointments" Height="72" HorizontalAlignment="Left" Margin="9,6,0,0" Name="button1" VerticalAlignment="Top" Width="441" Click="button1_Click" />
<TextBlock Height="277" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="29,108,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="407" />
</Grid>
Now in the code behind button event let us write the code which basically creates an instance of the appointment and do a async search to get the list of appointment from the local device as shown in the code below. Before doing that we need to add the reference for the user data at the top of the file (using Microsoft.Phone.UserData;)
Code:
[code:c#]private void button1_Click(object sender, RoutedEventArgs e)
{
Appointments f5debugAppointment = new Appointments();
f5debugAppointment.SearchCompleted += new EventHandler<AppointmentsSearchEventArgs>(Appointments_SearchCompleted);
DateTime dtsart = DateTime.Now;
DateTime dtend = dtsart.AddDays(10);
int max = 20;
f5debugAppointment.SearchAsync(dtsart, dtend, max, "My F5debug Appointment");
}
void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e)
{
MessageBox.Show(e.Results.Count().ToString());
}
Now we are done with our configuration, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs.