Dial a phone number from within your Windows Phone 7 application
Oct
18
Written by:
10/18/2011 9:04 AM
A Windows Phone 7 device will not let your application silently dial a number but you can initiate the process of making a phone call from your application and have the user confirm that the call should be made. Let us take a look at how you can dial a phone number via code.
You will need to create a basic Windows Phone 7 application in order to test the code supplied within this article. Please refer to our article entitled: "Creating your first Windows Phone 7 application" for additional assistance.
Once you have created your basic Phone 7 application, add a "TextBox" and a "Button" to your main screen. The "TextBox" will contain the number to dial and when the user clicks the "Button" the call will be initiated. We will call the "TextBox", "textbox1" and we will call the "Button", "button1".
Add the following "Using" statement to the "Using" section of your code-behind source code file:
1.using Microsoft.Phone.Tasks;
Double-click on the button called "button1", and replace your "button1_Click" event with the following:
1.private void button1_Click(object sender, RoutedEventArgs e)
2.{
3.
4. PhoneCallTask phoneTask = new PhoneCallTask();
5. phoneTask.PhoneNumber = textbox1.Text;
6. phoneTask.Show();
7.
8.}
Run your application to see the results.
The first line creates a "PhoneCallTask". The next line assigns the text from the "TextBox" called "textbox1" to the "PhoneNumber" property of the "PhoneCallTask" object. The final line runs the task. You can also use the "DisplayName" property of the "PhoneCallTask" object to display a user friendly name for the number being dialed. For example:
01.private void button1_Click(object sender, RoutedEventArgs e)
02.{
03.
04. PhoneCallTask phoneTask = new PhoneCallTask();
05. phoneTask.PhoneNumber = textbox1.Text;
06. phoneTask.DisplayName = "Police";
07. phoneTask.Show();
08.
09.}
You will notice that when you run the code provided that a dialog box is displayed to the user, asking the user if the device should call the phone number provided. The user can accept or decline the request.
In version 6.5 of the .NET Compact Framework(the version of the OS just before Windows Phone 7) you could also dial a number from within your phone application by simply using the following code:
01.//place this line in the using section
02.using Microsoft.WindowsMobile.Telephony;
03.
04.//add this function to the main class
05.private void PlaceCall()
06.{
07. Phone myPhone= new Phone();
08. myPhone.Talk(“1234567890″);
09.}
10.
11.//to call the function simple call PlaceCall(); from within one of your own functions
Thankfully Windows Phone 7 has made it just as easy to place phone calls from within our own applications.
Enjoy!