Get Address from Contact in Windows Phone 7.1 [Mango]

In this post I will show you, how you could get Address from contact and Display it.

Expected Output

  1. On running of application you will get a button
  2. On click of button Contact list will be open
  3. On selection of a contact . address of that contact will be displayed in label.

imageimageimage

Design the page

I am going to put a very simple design. Design would have just one button and label.

On click event of the button, contact list would be open and on user selection of a contact, we will display address of that contact in label.  We are going to do very simple stuff . Don’t we ? Smile

Content Grid of MainPage.Xaml would be looking like below,

image

Write the Code

Add namespace

clip_image001

Globally defined a variable of type AddressChooserTask

 

clip_image002

 

In constructor of page create instance of AddressChooserTask and register completed event on that.

 

clip_image004

 

In the event, check whether TaskResult is equal to TaskResult enum OK. If yes then display name and address in label.

 

clip_image006

 

On click event of button, we will show the contact using addressChooserTask.Show

image

 

For you reference full source code of code behind is as below,

MainPage.Xaml.cs


using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;


namespace AddressChooser
{
    public partial class MainPage : PhoneApplicationPage
    {

        AddressChooserTask addressChooserTask;

        public MainPage()
        {
            InitializeComponent();
            addressChooserTask = new AddressChooserTask();
            addressChooserTask.Completed += new EventHandler<AddressResult>(addressChooserTask_Completed);

        }

        private void addressChooserButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                addressChooserTask.Show();
            }
            catch (System.InvalidOperationException ex)
            {

            }

        }

        void addressChooserTask_Completed(object sender, AddressResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                txtDisplay.Text = "The address for " + e.DisplayName + " is " + e.Address;
            }

        }
    }
}

Now when you run phone application you should get the expected output.

I hope this post was useful. Thanks for reading  Smile

5 responses to “Get Address from Contact in Windows Phone 7.1 [Mango]”

  1. whts the new thing here? i cn find similr post on net

  2. Dhananjay Kumar

    With all Due respect to you sir , if you search on Google “Get Address from Contact in Windows Phone 7.1” My blog would come as First result .. now what to say ? I write to help people. I don’t try to be unique. Thanks for Reading this post

  3. How should add reference windows.phone.Task;

    i am can ‘t able to find AddresschooserTask classs

  4. This requires user interaction for choosing contacts. Is there a way to read contact/calendar from application silently without user interaction?

  5. […] Get Address from Contact in Windows Phone 7.1 [Mango] […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com