次の方法で共有


How to access calendar data for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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.

Accessing calendar data

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 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;
    
    Imports 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());
    }
    
    Private Sub ButtonAppointments_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    
        Dim appts As Appointments = New Appointments()
    
        'Identify the method that runs after the asynchronous search completes.
        AddHandler appts.SearchCompleted, AddressOf Appointments_SearchCompleted
    
        Dim start as DateTime = DateTime.Now
        Dim endTime as DateTime = start.AddDays(7)
        Dim max as integer = 20
    
        'Start the asynchronous search.
        appts.SearchAsync(start, endTime, max, "Appointments Test #1")
    End Sub
    
    Private Sub Appointments_SearchCompleted(sender As Object, e As AppointmentsSearchEventArgs)
    
        'Do something with the results.
        MessageBox.Show(e.Results.Count().ToString())
    End Sub
    

Data-binding appointment data results

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";
        }
    }
    
    Private Sub Appointments_SearchCompleted(sender As Object, e As AppointmentsSearchEventArgs)
    
        Try
            'Bind the results to the user interface.
            AppointmentResultsData.DataContext = e.Results
    
        Catch ex As System.Exception
    
            'No results
        End Try
    
        If AppointmentResultsData.Items.Any() Then
    
            AppointmentResultsLabel.Text = "results"
        Else
            AppointmentResultsLabel.Text = "no results"
        End If
    End Sub
    
  3. Save and build your solution.

  4. Start your application and click the button.

    The data appears in the list box.

Enumerating appointment data results

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());
    }
    
    Private Sub Appointments_SearchCompleted(sender As Object, e As AppointmentsSearchEventArgs)
    
        Dim sb As System.Text.StringBuilder = new System.Text.StringBuilder()
    
        For Each appt As Appointment In e.Results
    
            sb.AppendLine(appt.Subject)
        Next
    
        MessageBox.Show(sb.ToString())
    End Sub
    
  2. Save and build your solution.

  3. Start your application and click the button.

    The message box appears.

LINQ

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
        }
    }
    
    Private Sub Appointments_SearchCompleted(sender As Object, e As AppointmentsSearchEventArgs)
    
        Try
            'Bind the results to the user interface.
            AppointmentResultsDataLINQ.DataContext = _
                From appt As Appointment In e.Results _
                Where appt.IsAllDayEvent = false _
                Select appt
    
        Catch ex As System.Exception
    
            'No results
        End Try
    End Sub
    
  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.

See Also

Reference

Microsoft.Phone.UserData

Appointments..::.SearchAsync

Appointments..::.SearchCompleted

Other Resources

Walkthrough: Accessing contact and calendar data for Windows Phone 8

How to access contact data for Windows Phone 8

Code Samples for Windows Phone