Sdílet prostřednictvím


How to use the phone number chooser task for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Use the phone number Chooser task to obtain the phone number of a contact selected by the user. This task launches the Contacts application so that the user can choose a contact. If the user completes the task, an event is raised and the event handler receives a phone number in the result.

By using Choosers, you help provide a consistent user experience throughout the Windows Phone platform. For more information, see Launchers and Choosers for Windows Phone 8.

Note

The Windows Phone SDK includes APIs that you can use to retrieve contact information from within your application. For more information, see Contacts and Calendar for Windows Phone 8.

To use the phone number Chooser task

  1. Add the following statement to your code.

    using Microsoft.Phone.Tasks;
    
    Imports Microsoft.Phone.Tasks
    
  2. Declare the task object. It must have page scope, so declare it in your page before the constructor.

    PhoneNumberChooserTask phoneNumberChooserTask;
    
    Dim phoneNumberChooserTask As PhoneNumberChooserTask
    
  3. Add the following code to your page constructor. This code initializes the task object, and identifies the method to run after the user completes the task.

    phoneNumberChooserTask = new PhoneNumberChooserTask();
    phoneNumberChooserTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask_Completed);
    
    phoneNumberChooserTask = new PhoneNumberChooserTask()
    AddHandler phoneNumberChooserTask.Completed, AddressOf phoneNumberChooserTask_Completed
    
  4. Add the following code to your application wherever you need it, such as in a button click event. To test this procedure, you can put the code in the page constructor. This is the code to launch the task.

    phoneNumberChooserTask.Show();
    
    phoneNumberChooserTask.Show()
    
  5. Add the code for the completed event handler to your page. This code runs after the user completes the task. The result is a PhoneNumberResult object that contains the name and phone number of the contact. There is sample code to start a new call using the retrieved phone number. For more information, see How to use the phone call task for Windows Phone 8.

    void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            MessageBox.Show("The phone number for " + e.DisplayName + " is " + e.PhoneNumber);
    
            //Code to start a new call using the retrieved phone number.
            //PhoneCallTask phoneCallTask = new PhoneCallTask();
            //phoneCallTask.DisplayName = e.DisplayName;
            //phoneCallTask.PhoneNumber = e.PhoneNumber;
            //phoneCallTask.Show();
        }
    }
    
    Private Sub phoneNumberChooserTask_Completed(sender As Object, e As PhoneNumberResult)
    
        If e.TaskResult = TaskResult.OK
    
            MessageBox.Show("The phone number for " + e.DisplayName + " is " + e.PhoneNumber)
    
            'Code to start a new call using the retrieved phone number.
            'Dim phoneCallTask As PhoneCallTask = New PhoneCallTask()
            'phoneCallTask.DisplayName = e.DisplayName
            'phoneCallTask.PhoneNumber = e.PhoneNumber
            'phoneCallTask.Show()
        End If
    End Sub
    

See Also

Reference

PhoneNumberChooserTask

Completed

ContactPhoneNumber

Other Resources

How to use the save phone number task for Windows Phone 8