|
In this article we will discuss how to select or choose numbers from phone directory of Windows Phone 7 and display selected number on the screen. We will use PhoneNumberChooserTask of Microsoft.Phone.Tasks.
Let's write code:
Step 1: Create Windows Phone Application of type Silvelight for Windows Phone.

Step 2: Place a button and a textblock inside content grid. The button will be used to access the phone contacts and textblock will be used to display the selected contacts.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button Content="Get Phone Number" Height="72" HorizontalAlignment="Left" Margin="12,6,0,0" Name="GetPhoneNumber" VerticalAlignment="Top" Width="300" Click="GetPhoneNumber_Click" /> <TextBlock Name="txtPhoneNumber" Margin="27,100,0,0" FontSize="28" Text="Phone Number: " /> </Grid>
Step 3: Add using Microsoft.Phone.Tasks;
Step 4: Create a class level variable of PhoneNumberChooser.
PhoneNumberChooserTask numberChooser Step 5: Create a instance of numberChooser and attach EventHandler to it in the MainPage constructor.
// Constructor public MainPage() { InitializeComponent(); numberChooser = new PhoneNumberChooserTask(); numberChooser.Completed += new EventHandler<PhoneNumberResult>(numberChooser_Completed); }
Step 6: GetPhoneNumber_Click will be triggered on click of Get Phone Number Button.
private void GetPhoneNumber_Click(object sender, RoutedEventArgs e) { //Start the Phone Number Chooser. numberChooser.Show(); }
Below screen will be displayed on trigger of above event which will let the user to select contacts.

PhoneNumberChooser also gives option of search from contact list. On click of search icon in the above displayed screen, the contacts search option will appear.

Step 7: Now add the numberChooser_Completed which will be triggered once you select the number from Phone contact list and the selected contact number wil be displayed in the text block.
void numberChooser_Completed(object sender, PhoneNumberResult e) { // Check if TaskResult is a success if (e.TaskResult == TaskResult.OK) { // Display the phone number in the TextBlock txtPhoneNumber txtPhoneNumber.Text += e.PhoneNumber; } }

This ends the article of accessing Phone contact list in Windows Phone 7.
|