Saturday, October 8, 2011

How to make a call in Windows Phone 7

 

00

In this article, I am going to describe how to make a call in windows phone 7 using managed API.

Objective of this article to make a call programmatically, sometimes application has specific requirement to make a phone call from application; in that scenario we can take the leverage of Microsoft managed API Microsoft.Phone.Tasks (namespace) allows to access PhoneCallTask API to make a call in windows phone.

Basically PhoneCallTask API allows an application to launch the Phone application. Use this to allow users to make a phone call from your application.

Let’s take an example to make a phone call from application. Add a Button to the default XAML which is gifted by MS Visual Studio when you start a new Windows Phone 7 Silverlight application. See the below xaml here.

 

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
      <Button Content="Phone Call" Height="82" HorizontalAlignment="Left" Margin="140,234,0,0" Name="btnCall" VerticalAlignment="Top" Width="auto" Click="btnCall_Click" />
</Grid>

XAML will generate the UI (user interface) that look like see below screen.

01

Now initialize the PhoneCallTask task object in the constructor of page, to avoid the memory overhead so I initialized in the constructor.

PhoneCallTask phoneTask = null;
// Constructor
public MainPage()
{
    InitializeComponent();
    phoneTask = new PhoneCallTask();            
}

The PhoneCallTask class contains two core properties like DisplayName, PhoneNumber and one show method, which allows us to shows the phone application. To make an actual call though the application.

Now double click the button control and click event handler got added to button control object. In the button click event set the display name and phone number which you want to make a call. And calls a *Show()* method of phone task object. See below mentioned code snippet.

private void btnCall_Click(object sender, RoutedEventArgs e)
{
  phoneTask.DisplayName = "Pavan Pareta";
  phoneTask.PhoneNumber = "+911234567890"; // put your desired phone number here
  phoneTask.Show();
}

Now the application is get ready to execute. When the user clicks on the button, phone dial will appear the phone user has to click the on *call* button then only we can make a call.

See blow screenshots

0203

Download source code here.

Thank you for your time.