Floating FB popout byF5debug

101 How to on Windows Phone – How to #44 – Programmatically access Contact Data in Windows Phone

 

imageIn 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.

2013-01-21_16h19_16

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.

2013-01-21_17h33_34

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>

[/code]

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.

2013-01-22_11h40_59

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)
            {
            }
        }

[/code]

Output:

image

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+

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter