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

The computer thinks these might be related:
  1. Windows Phone Videos
  2. Windows Phone Fundamentals–3 Ways To Handle Events
  3. Windows Phone–Sending Messages Between Pages
  4. Windows Phone Fundamentals–Layout
This entry was posted in Mango, Mini-Tutorial, WindowsPhone and tagged . Bookmark the permalink.

9 Responses to Obtaining Email, Address or Phone Number

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

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

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

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

  5. Dale says:

    Great mini tutorial Jesse.

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>