In my last article i discussed about Part 19 - Windows Phone 7 - Access Phone Contacts, we discussed about how to access phone contacts. In this article we will discuss how to make call once you get the phone contact details.
Let's write code:
Step 1: Create Windows Phone Application of type Silvelight for Windows Phone.

Step 2: Place two buttons and 4 textblock inside content grid. The first button will be used to access the phone contacts and the second button will be used to make call. Two textblocks will be used to display "Display Name" and "Phone Number" label and the rest of the two texblocks will be used to display the selected contact name and phone number.
<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="txtDisplayNameLabel" Margin="27,100,0,0" FontSize="28" Text="Display Name: " /> <TextBlock Name="txtDisplayName" Margin="220,100,0,0" FontSize="28" Text="" /> <TextBlock Name="txtPhoneNumberLabel" Margin="27,180,0,0" FontSize="28" Text="Phone Number: " /> <TextBlock Name="txtPhoneNumber" Margin="230,180,0,0" FontSize="28" Text="" /> <Button Content="Call" Height="72" HorizontalAlignment="Left" Margin="27,240,0,0" Name="Call" VerticalAlignment="Top" Width="200" Click="Call_Click" /> </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(); }
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; txtDisplayName.Text += e.DisplayName; } }
Step 8: Below method will call the selected phone number on click of Call method. PhoneCallTask class of Microsoft.Phone.Tasks will be used.
void Call_Click(object sender, RoutedEventArgs e) { PhoneCallTask phoneCallTask = new PhoneCallTask(); // Contact information to pass to the phone dialer application. phoneCallTask.PhoneNumber = txtPhoneNumber.Text; phoneCallTask.DisplayName = txtDisplayName.Text; // Launches the phone dialer application. phoneCallTask.Show(); }
Below screen will appear on click of Call button.
On click of Call button of above image, call will be placed.

This ends the article of accessing phone contacts and placing call.
|