The Appointments
class allows you to retrieve appointments that fall between two specified dates.
Note
To access the user’s appointments, the ID_CAP_APPOINTMENTS capability must be present in the WMAppManifest.xml file.
For more information on the phone’s security capability model, see Chapter 2.
To query the user’s appointments, define an Appointments
instance as a field in a class, as shown:
Appointments appointments;
Instantiate the Appointments
object, subscribe to its SearchCompleted
event, and then call SearchAsync
. The following excerpt demonstrates the retrieval of appointments that fall within the next six months:
void SearchAppointments()
{
appointments = new Appointments();
appointments.SearchCompleted += HandleSearchCompleted;
appointments.SearchAsync(DateTime.Now, DateTime.Now.AddMonths(6), null);
}
When the search completes, the specified event handler receives a list of matching Appointment
objects. See the following excerpt:
void HandleSearchCompleted(object sender, AppointmentsSearchEventArgs e)
{
if (e.Results == null)
{
return;
}
Appointments = e.Results;
}
To determine the accounts from which appointments are available, use the Appointment
class’s Accounts
property.
Each appointment is represented by an Appointment
object. The Appointment
class provides various read-only properties, described in Table 14.2.
The Appointments.SearchAsync
method is overloaded to allow you to restrict the retrieval of appointments from specific accounts. For example, to retrieve only those appointments associated with the user’s Windows Live account, use the following:
Account account = appointments.Accounts.Where(
x => x.Name == "Windows Live").First();
appointments.SearchAsync(
DateTime.Now, DateTime.Now.AddMonths(6), account, null);
The example for this section allows the user to retrieve the list of appointments for a specified period. The AppointmentsViewModel
class, located in the downloadable sample code, contains a StartTimeInclusive
property and an EndTimeInclusive
property, which are used to specify the period (see Listing 14.13).
A DelegateCommand
named SearchCommand
calls the SearchAppointments
method when it is executed. When the SearchCompleted
handler is called, the Appointments
property is populated with the result.
public class AppointmentsViewModel : ViewModelBase
{
public AppointmentsViewModel() : base("appointments")
{
searchCommand = new DelegateCommand(arg => SearchAppointments());
}
Appointments appointments;
void SearchAppointments()
{
appointments = new Appointments();
appointments.SearchCompleted += HandleSearchCompleted;
appointments.SearchAsync(startTimeInclusive, endTimeInclusive, null);
}
void HandleSearchCompleted(object sender, AppointmentsSearchEventArgs e)
{
if (e.Results == null)
{
return;
}
Appointments = e.Results;
}
IEnumerable<Appointment> appointmentList = new List<Appointment>();
public IEnumerable<Appointment> Appointments
{
get
{
return appointmentList;
}
private set
{
Assign(ref appointmentList, value);
}
}
readonly DelegateCommand searchCommand;
public ICommand SearchCommand
{
get
{
return searchCommand;
}
}
DateTime startTimeInclusive = DateTime.Now.AddYears(-1);
public DateTime StartTimeInclusive
{
get
{
return startTimeInclusive;
}
set
{
Assign(ref startTimeInclusive, value);
}
}
DateTime endTimeInclusive = DateTime.Now.AddYears(1);
public DateTime EndTimeInclusive
{
get
{
return endTimeInclusive;
}
set
{
Assign(ref endTimeInclusive, value);
}
}
}
The view contains two Windows Phone Toolkit ListPicker
controls that are bound to the viewmodel’s StartTimeInclusive
and EndTimeInclusive
properties (see Listing 14.14).
Results are displayed using a ListBox
, whose ItemsSource
property is bound to the viewmodel’s Appointments
property. Each Appointment
object is presented using a TextBlock
to display its StartTime
, Subject
, and Details
properties.
<StackPanel x:Name="ContentPanel" Grid.Row="1">
<toolkit:DatePicker Value="{Binding StartTimeInclusive, Mode=TwoWay}"
Header="start"/>
<toolkit:DatePicker Value="{Binding EndTimeInclusive, Mode=TwoWay}"
Header="end"/>
<ListBox ItemsSource="{Binding Appointments}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,20">
<TextBlock Text="{Binding StartTime}"
Style="{StaticResource PhoneTextNormalStyle}" />
<TextBlock Text="{Binding Subject}"
Style="{StaticResource PhoneTextLargeStyle}" />
<TextBlock Text="{Binding Details}"
Style="{StaticResource PhoneTextNormalStyle}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
In addition, the view includes an AppBarIconButton
that is bound to the viewmodel’s SearchCommand
, as shown:
<u:AppBar>
<u:AppBarIconButton
Command="{Binding SearchCommand}"
Text="search"
IconUri="/Images/ApplicationBarIcons/AppBarSearch.png" />
</u:AppBar>
When the user taps the application bar button, appointments that fall within the specified date range are displayed at the bottom of the page (see Figure 14.38).
By using the Appointments
and Contacts
classes, you are able to further integrate your app with the social networking features of the phone.
18.222.80.236