Obtaining Email, Address or Phone Number

Mango From Scratch

Windows Phone Mango now has new tasks to obtain additional information from the tasks contacts list, such as the user’s email address, snail-mail address and phone number.

All of these work essentially in the same way: you create the chooser and show it. The contact list is displayed. You select the contact, and you get back the information in the event args.  The following simple program illustrates.

Create a Windows Phone application and on MainPage.xaml create five rows in the ratio 1:1:1:4:1. Populate the first three rows with buttons and the fourth row with a TextBlock (leaving the final row empty).  Here’s the complete Xaml,

<Grid
    x:Name="ContentPanel"
    Grid.Row="1"
    Margin="12,0,12,0">
    <Grid.RowDefinitions>
        <RowDefinition
            Height="1*" />
        <RowDefinition
            Height="1*" />
        <RowDefinition
            Height="1*" />
        <RowDefinition
            Height="3*" />
        <RowDefinition
            Height="1*" />
    </Grid.RowDefinitions>
    <Button
        Grid.Row="0"
        Name="FindEmail"
        Content="Find Email"
        VerticalAlignment="Center"
        HorizontalAlignment="Center" />
    <Button
        Name="FindAddress"
        Content="Find Address"
        VerticalAlignment="Center"
        HorizontalAlignment="Center" 
        Grid.Row="1"/>
    <Button
        Grid.Row="2"
        Name="FindPhone"
        Content="Find Phone Number"
        VerticalAlignment="Center"
        HorizontalAlignment="Center" />
    <TextBlock
        Name="Results"
        Grid.Row="3"
        TextWrapping="Wrap"
        Margin="0,50,0,0"
        FontSize="48"
        Style="{StaticResource PhoneTextTitle1Style}"/>
</Grid>

Note the bit of styling on the TextBlock.

At the top of the code-behind page declare the three choosers as member variables of the class,

public partial class MainPage : PhoneApplicationPage
{
    AddressChooserTask addressTask;
    EmailAddressChooserTask emailTask;
    PhoneNumberChooserTask phoneTask;

 

Each button’s event handler will instantiate one of the choosers and set its completed event handler, and then call its Show method to launch the task. For example, the FindEmail button’s click event handler looks like this,

 FindEmail.Click += ( o, e ) =>
 {
     emailTask = new EmailAddressChooserTask();
     emailTask.Completed += 
          new EventHandler<EmailResult>( emailTask_Completed );
     emailTask.Show();
 };

Similarly, the other two Button click event handlers are very much the same,

FindAddress.Click += ( o, e ) => 
{
    addressTask = new AddressChooserTask();
    addressTask.Completed += 
        new EventHandler<AddressResult>( addressTask_Completed );
    addressTask.Show(); 
};

FindPhone.Click += ( o, e ) =>
{
    phoneTask = new PhoneNumberChooserTask();
    phoneTask.Completed += 
      new EventHandler<PhoneNumberResult>( phoneTask_Completed );
    phoneTask.Show();
};

 

In each case you’ve written an event handler for the completed event.  The pattern is the same for each: examine the TaskResult to ensure that nothing has gone amiss, and then extract the relevant information from the event args to display in the Results TextBlock,

void phoneTask_Completed( object sender, PhoneNumberResult e )
{
    if (e.TaskResult == TaskResult.OK)
    {
        Results.Text = e.DisplayName + ": " + e.PhoneNumber;
    }
}

void emailTask_Completed( object sender, EmailResult e )
{
    if (e.TaskResult == TaskResult.OK)
    {
        Results.Text = e.DisplayName + ": " + e.Email;
    }
}

void addressTask_Completed( object sender, AddressResult e )
{
    if (e.TaskResult == TaskResult.OK)
    {
        Results.Text = e.DisplayName + ": " + e.Address;
    }
}

 

When you run the application and click on one of the buttons you are taken to the contacts. If you choose a contact the relevant information is displayed on the home page.

Share

About Jesse Liberty

Jesse Liberty is an independent consultant and programmer with three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP, Microsoft MVP and Telerik MVP.
This entry was posted in Mango, Mini-Tutorial, WindowsPhone and tagged . Bookmark the permalink.

12 Responses to Obtaining Email, Address or Phone Number

  1. extenstion says:

    Saved aas a favorite, I likе yօur website!

    Stߋp by my pagе: extenstion

  2. Timothy Mann says:

    In your book “Teach Yourself C++ in 24 Days” listings 19.4, 19.5, 19.6 and 19.7 do not compile wih g++ on linux. For example, the following message results from trying to compile list19.4:
    g++ -fpermissive list1904.cpp -o List19.4
    list1904.cpp:45:52: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Array&)’ declares a non-template function [-Wnon-template-friend]
    list1904.cpp:45:52: note: (if this is not what you intended, make sure the function template has already been declared and add after the function name here)
    /tmp/ccrv8ob6.o: In function `main’:
    list1904.cpp:(.text+0x102): undefined reference to `operator<<(std::ostream&, Array&)’
    collect2: error: ld returned 1 exit status
    make: *** [List19.4] Error 1

    Can you clarify this problem? Thank you very much.

    Timothy Mann
    T
    CC

  3. Hi Jesse,
    Nice article. Just want to know one more thing can we import bulk contact in phone excluding save dialog where user have to press save or cancel ?

    Just one click and all the contact saved in phone.

    Thanks in advance for reply.

  4. Renaud says:

    Hi Jesse,

    One question about the contact list : will we be able to access the contact list directly without asking the user to choose the contact itself ? I am thinking about the scenario where you loop through the list to find if the current user already have friends with a phone number present in the application database (in order to link people together).

    Thanks

  5. Pingback: Windows Phone Mango: Obtaining Email, Address or Phone Number – www.nalli.net

  6. Pingback: Obtaining Email, Address or Phone Number | WP7 Developers Links

  7. Jorge Levy says:

    Thanks for the info Jesse…

    Just one question: can we test this on WP7 emulator? How?

    Thanks…

    • Sure. I’m not sure what you mean by “how” though; it just works.

      • How would one add any contacts to emulator to test it,as it has only Internet Explorer and Settings(Date, region and theme) available there? Same problem with music player and many other features – there’s no way to import any data/media to phone for testing purposes.

  8. Dale says:

    Great mini tutorial Jesse.

  9. Hey Jesse,

    The EmailAddressChooserTask and the PhoneNumberChooserTask were already available in WP7.0, the AddressChooserTask is new.

    Cheers,
    Laurent

Leave a Reply

Your email address will not be published.