Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 7 – Working with Calendar in WP7

 

Introduction:

In this article we are going to see how to use the Calendar application  to access the calendar data and use it across the development as and when needed. In our earlier article we have seen how to use the contacts and access the contact data, we are going to use the same process of accessing the calendar objects.

In order to use the calendar object we are going to use the reference of the Appointments object and do an Async search to get the results and show it on to a collection of Appointment objects. The end result in the appointments object can be used in different ways as per the requirement by binding to different controls and to do some schedule. Appointment class is to interact with user appointment data and is inherited from the Microsoft.Phone.UserData namespace. Let us see the step by step process on how to use the calendar objects and list the appointments.    [more]

Steps:

Open Visual Studio 2010 and create a new Silverlight for Windows Phone 7 application with a valid project name as shown in the screen below.

clip_image002

Now in order to fetch the details of the calendar appointments and show it to the end users, we will add controls from the tools as shown in the screen below. We can just drag and drop the controls from the tool box or write the XAML code as shown in the screen below or copy paste the XAML code.


XAML Code:

01

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

02

<TextBlock x:Name="ApplicationTitle" Text="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>

03

<TextBlock x:Name="PageTitle" Text="calendar" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

04

</StackPanel>

05

 

06

<!--ContentPanel - place additional content here-->

07

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

08

<Button Content="Get Appointment" Height="83" HorizontalAlignment="Left" Margin="30,16,0,0" Name="btnAppointment" VerticalAlignment="Top" Width="402" Click="btnAppointment_Click" />

09

<ListBox Name="lstAppointment" ItemsSource="{Binding}" Margin="47,188,36,52" >

10

<ListBox.ItemTemplate>

11

<DataTemplate>

12

<TextBlock Name="txtResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />

13

</DataTemplate>

14

</ListBox.ItemTemplate>

15

</ListBox>

16

<TextBlock Height="41" HorizontalAlignment="Left" Margin="47,118,0,0" Name="txtAppointments" Text="List of Appointments" VerticalAlignment="Top" Width="373" />

17

</Grid>

18

</Grid>

clip_image004

Now we need to add code to do listing of the calendar appointments to the list view. First add the namespace

1

using Microsoft.Phone.UserData;

Now add the button click event code, here we are going to create an object of the Appointment class and do an AsyncSearch. We need to provide the start time and end time to fetch the appointment details and also should provide the maximum count. Copy and paste the below code to the code behind for the button click event.


C# Code:

01

private void btnAppointment_Click(object sender, RoutedEventArgs e)

02

{

03

 

04

Appointments aAppointment = new Appointments();

05

 

06

aAppointment.SearchCompleted += new EventHandler<AppointmentsSearchEventArgs>(GetAppointments);

07

 

08

DateTime starttime = DateTime.Now;

09

DateTime endtime = starttime.AddDays(10);

10

int maxAppointment = 20;

11

 

12

aAppointment.SearchAsync(starttime, endtime, maxAppointment, null);

13

}

14

 

15

void GetAppointments(object sender, AppointmentsSearchEventArgs e)

16

{

17

try

18

{

19

lstAppointment.DataContext = e.Results;

20

}

21

catch (System.Exception)

22

{

23

txtAppointments.Text = "No Appointments Found!!!";

24

}

25

 

26

if (lstAppointment.Items.Any())

27

{

28

txtAppointments.Text = "Below is the List of Appointments";

29

}

30

else

31

{

32

txtAppointments.Text = "No Appointments Found!!!";

33

}

34

}

Now the above code will pull the information from the appointments object and bind it to the data context of the list box. If there is no appointments available we can see an empty result as well. To check the application just run the application by pressing F5 directly from the keyboard or by selecting build and execute solution from the tool bar and we can see the screen as shown in the screen below.

clip_image006

Since we are in the emulator and we don’t have any appointments saved the list is empty. If any appointments are saved in the physical device we will get the complete list of the next 10 days to be displayed here in the list box.

Conclusion:

So in this article we have seen how to use the calendar object to fetch the appointment details and list it out. We can customize to use the appointments object to bind to different data binding options as per the requirements.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Advertisement

Random Posts

Most Viewed

BlogStatistic

Total posts: 617This year: 4This month: 2This week: 0Comments: 302

Month List

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter