How to read appointments or calendar in Windows phone

How to read appointments or calendar in Windows phone

This article describes how to read appointments or calendar  data programmatically.

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

Accessing calendar data

To accessing calendar data is to get a reference to the Appointments object. It do an asynchronous search on it, and then capture the results as a collection of Appointment objects.

Include the ID_CAP_APPOINTMENTS capability in app manifest file.

 Create a new Windows Phone App and then select Windows Phone 8.0 as platform you want to target

Go to Solution Explorer , expand the Properties folder, and then double-click WMAppManifest.xml

Click on the Capabilities tab of the manifest designer, select the check box next to ID_CAP_APPOINTMENTS




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

 

MainPage.Xaml

<phone:PhoneApplicationPage

    x:Class="AppointmentsSln.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation ="Portrait"

    shell:SystemTray.IsVisible="True">

    <!--Listbox Template-->

    <phone:PhoneApplicationPage.Resources>          

        <DataTemplate x:Key="AptItemTemplate">

            <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,10">

                <Grid.ColumnDefinitions>

                    <ColumnDefinition Width="95"/>

                    <ColumnDefinition Width="*"/>

                </Grid.ColumnDefinitions>

                <Border  Width="85" Height="85" Margin="5">

                    <TextBlock Text="{Binding StartTime}" Foreground="White" MaxHeight="60" TextWrapping="Wrap" FontWeight ="Bold"/>

 

                </Border>

                <StackPanel Grid.Column="1" Margin="10,0,0,0">

                    <TextBlock Text="{Binding Subject}" Foreground="White" MaxHeight="60" TextWrapping ="Wrap" FontWeight="Bold"/>

                    <TextBlock Text="{Binding Location}" Foreground="White" TextWrapping ="Wrap" />

                </StackPanel>

            </Grid>

        </DataTemplate>

    </phone:PhoneApplicationPage.Resources>

    <!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

 

         <!--TitlePanel contains the name of the application and page title-->

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

            <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle }" Margin="12,0"/>

            <TextBlock Text="Appointments" 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">

            <ListBox Name="AppointmentResultsData" Grid.Row="1" ItemsSource="{Binding}" ItemTemplate="{StaticResource AptItemTemplate }"  >

            </ListBox>

        </Grid>

    </Grid>

</phone:PhoneApplicationPage>

 

MainPage.xaml.cs

Add using statement in the top of the class

using Microsoft.Phone.UserData; 

Instantiate the Appointment object in the constructor of the class

public MainPage()

 {

Appointments appts = new Appointments();

  //Identify the method that runs after the asynchronous search completes.

appts.SearchCompleted += appts_SearchCompleted;

DateTime start = DateTime.Now;

DateTime end = start.AddDays(7); //get the appointment for next 7 days 

//get Maximun 20 Appointments.

int max = 20;

appts.SearchAsync(start, end, max, "Appointments Test #1");

}

 

    void appts_SearchCompleted(object sender, AppointmentsSearchEventArgs e)

        {

            try

            {

                AppointmentResultsData.DataContext =

                    from Appointment appt in e.Results

                    where appt.IsAllDayEvent == false

                    select appt;

                IEnumerable<Appointment> appointment = from Appointment appt in e.Results

                                                        where appt.IsAllDayEvent == false

                                                        select appt;

            }

            catch (System.Exception)

            {

                //No results

            }

        }

comments powered by Disqus

Related Articles