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.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was 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 Microsoft MVP.
This entry was posted in Mango, Mini-Tutorial, WindowsPhone and tagged . Bookmark the permalink.

10 Responses to Obtaining Email, Address or Phone Number

Comments are closed.