Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to access calendar data for Windows Phone 8

Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows Phone OS 7.1

The process for accessing calendar data is to get a reference to the Appointments object, perform an asynchronous search on it, and then capture the results as a collection of Appointment objects. You can use the results in different ways, and this topic demonstrates how to bind the results to the user interface or to enumerate through the results.

This topic discusses read-only access to user’s contact data. For information on creating a custom contact store for your app that provides read and write access, see Custom contact store for Windows Phone 8.

This topic contains the following sections.

In this procedure, you put the code in a button click event for testing purposes only. In your own applications, you can access calendar data wherever you need it. The following procedure assumes that you have a Windows Phone application that has a page with a button named ButtonAppointments. In this procedure, you search for all appointments in the next seven days.

Important noteImportant Note:

Windows Phone Emulator does not contain sample appointments. You should test these procedures using a physical device.

To access calendar data

  1. At the top of the code-behind file for your page, add the following statement.

    using Microsoft.Phone.UserData;
    
  2. Add the following code. It contains the button click event and the method to handle the completed event of the asynchronous search.

    private void ButtonAppointments_Click(object sender, RoutedEventArgs e)
    {
        Appointments appts = new Appointments();
    
        //Identify the method that runs after the asynchronous search completes.
        appts.SearchCompleted += new EventHandler<AppointmentsSearchEventArgs>(Appointments_SearchCompleted);
    
        DateTime start = DateTime.Now;
        DateTime end = start.AddDays(7);
        int max = 20;
    
        //Start the asynchronous search.
        appts.SearchAsync(start, end, max, "Appointments Test #1");
    }
    
    void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e)
    {
        //Do something with the results.
        MessageBox.Show(e.Results.Count().ToString());
    }
    

In this procedure, you data-bind the results of an appointment search directly to the user interface. This procedure assumes you have completed the procedure “Accessing Calendar Data” in the preceding section.

To data-bind appointment data

  1. Open the XAML editor for your page and add the following code. You can add the code to the Content Panel or Layout Root GRID element. This XAML creates a list box that you bind to the appointment data. The data template for each row of the list box contains the subject of the appointment. In your applications, you can create any data template that you like. For more information, see Data binding for Windows Phone 8 and Data Templating Overview.

    <StackPanel Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
    
        <TextBlock Name="AppointmentResultsLabel" Text="results are loading..." Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap" />
    
        <ListBox Name="AppointmentResultsData" ItemsSource="{Binding}" Height="200" Margin="24,0,0,0" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Subject, Mode=OneWay}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
    
  2. In the code-behind for your page, replace the existing event handler with the following code. This code binds the appointment data to the user interface by setting the data context of the list box equal to the results of the search. It includes error handling in case there are no results. The code also changes the text of the list box label depending on whether there are results or not.

    void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e)
    {
        try
        {
            //Bind the results to the user interface.
            AppointmentResultsData.DataContext = e.Results;
        }
        catch (System.Exception)
        {
            //No results
        }
    
        if (AppointmentResultsData.Items.Any())
        {
            AppointmentResultsLabel.Text = "results";
        }
        else
        {
            AppointmentResultsLabel.Text = "no results";
        }
    }
    
  3. Save and build your solution.

  4. Start your application and click the button.

    The data appears in the list box.

In this procedure, you enumerate the results of an appointment search and display the results in a message box. This procedure assumes you have completed the procedure “Accessing Calendar Data” in a preceding section of this topic.

To enumerate appointment data

  1. In the code-behind for your page, replace the existing event handler with the following code. This code enumerates the appointment data by using a for-each loop.

    void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
    
        foreach (Appointment appt in e.Results)
        {
            sb.AppendLine(appt.Subject);
        }
    
        MessageBox.Show(sb.ToString());
    }
    
  2. Save and build your solution.

  3. Start your application and click the button.

    The message box appears.

In this procedure, you use LINQ to refine the results of an appointment search. You data-bind the results directly to the user interface. This procedure assumes you have completed the procedure “Accessing Appointment Data” in a preceding section of this topic.

To use LINQ

  1. In the code-behind for your page, replace the existing event handler with the following code. This code queries the results for appointments that are not all-day appointments.

    void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e)
    {
        try
        {
            AppointmentResultsDataLINQ.DataContext =
                from Appointment appt in e.Results
                where appt.IsAllDayEvent == false
                select appt;
    
        }
        catch (System.Exception)
        {
            //No results
        }
    }
    
  2. Open the XAML editor for your page and add the following code. You can add the code to the Content Panel or Layout Root GRID element. This XAML creates a list box that you bind to the appointment data. Each row of the list box contains the subject of the appointment.

    <ListBox Name="AppointmentResultsDataLINQ" ItemsSource="{Binding}" Height="200" Margin="24,0,0,0" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Subject, Mode=OneWay}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    
  3. Save and build your solution.

  4. Start your application and click the button.

    The filtered data appears in the list box.

Show:
© 2014 Microsoft