In this tutorial we are going to see how to programmatically access the Contact data information in a Windows Phone application. As we know recently we have the newer version of Windows phone version 8 released which has a list of new features and enhancements. In our earlier article we have seen how to programmatically create a Panorama Control in Windows Phone which clearly explain how to use this control effectively in real time (Refer to the article – Programmatically create a Panorama Control in Windows Phone). [more]
Here we will see in detail on how to access the Contact information programmatically in an application we need to use the Contact Objects which performs an asynchronous search on to the collection. Let us see the step by step process on how to use this control in real time developing the Windows phone 8 application. Open Visual Studio 2012 IDE in administrator mode and create a new Windows Phone 7 project with a valid project name as shown in the screen below.
Once the project is initialized, we can see a list of default files and folders created. Now in the Mainpage.xaml let us add some controls which are used to show the contact information, basically we are going to run the application in the Emulator which is the inbuilt data that can be showed while running this application. So the XAML code looks like below which has a Button control to trigger the event on demand, and a List box control to show the details as shown in the screen below.
XAML Code:
[code:c#]<Grid x:Name=”ContentPanel” Grid.Row=”1″ Margin=”12,0,12,0″>
<StackPanel Height=”Auto” Width=”Auto” HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” >
<Button Name=”btnGetResult” Width=”200″ Height=”100″ Content=”Get Contact”>
</Button>
<ListBox x:Name=”ContactResultsData” ItemsSource=”{Binding}” Height=”200″ Margin=”24,0,23,0″ >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name=”ContactResults” Text=”{Binding DisplayName, Mode=OneWay}” />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
In the button click event write the below code, which basically creates an instance of the Contact class and do an asynchronous search as shown in the screen below.
C# Code:
[code:c#]private void btnGetResult_Click(object sender, RoutedEventArgs e)
{
Contacts cons = new Contacts();
cons.SearchAsync(string.Empty, FilterKind.None, ” “);
cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
}
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
try
{
ContactResultsData.DataContext = e.Results;
}
catch (System.Exception)
{
}
}
Output:
The button click event has the code which creates an instance of the Contacts class and the method handles the completed event of the async search. On the completed event we have written a code to bind the data to the user interface as shown in the code above. Now build and execute the application and we can see the list of available contacts that can be used in your application for any internal usage to handle the contact details.
Hope this tutorial is useful to you, see you all in the next tutorial. Provide us your support by Liking us on Facebook or Following us on Twitter or Adding us on Google+