How to Add a New Contact, Make a Call and Send SMS in Windows Phone 7


In this article you will see how to add a new contact, make a call and send SMS to it in Windows Phone 7.

Introduction

In this article we will use different Tasks provided by Windows Phone 7 to add a new contact to the phone contact list, make a call and send SMS to that contact.

A Task is used to get information about a module from the phone. To use a Task, you need to add Microsoft.Phone.Tasks namespace.

Adding New Contact

Create an instance of SaveContactTask

SaveContactTask newContact = new SaveContactTask();

Declare the Completed event of this instance to show a message if the contact has been added:

newContact.Completed+= new EventHandler<SaveContactResult>(newContact_Completed);

Provide values to the SaveContactTask instance; you want to save to the new contact. Show is called here to launch the contact application with the values you given.

private void btnSaveEmail_Click(object sender, RoutedEventArgs e)
{
    newContact.FirstName = "Deepak";
    newContact.LastName = "Sharma";
    newContact.MobilePhone = "99********";
    newContact.Show();
}

void  newContact_Completed(object sender, SaveContactResult e)
{
    if(e.TaskResult== TaskResult.OK)
    {
        MessageBox.Show("Contact added!");
    }
    else
    {
        MessageBox.Show("Contact not added!");
    }
}

The newContact.Show method launches the contacts application. Tap on the save icon to save it to the phone contact list. A message, "Contact added!" is displayed when the contact is saved else it shows a "Contact not added!" message.

SMSWinPhn1.gif

Making a Call

PhoneCallTask is used to make a call

private void btnCall_Click(object sender, RoutedEventArgs e)
{
    PhoneCallTask phoneCaller = new PhoneCallTask();
    phoneCaller.PhoneNumber = "99********";
    phoneCaller.DisplayName = "dpkshr";
    phoneCaller.Show();
}

Here, the Show method launches the phone application that takes your permission to make the call. Tap on the call button to call or the don't call button to cancel.

SMSWinPhn2.gif

Sending SMS

SMSComposeTask is used to send SMS

private void btnSendSMS_Click(object sender, RoutedEventArgs e)
{
    SmsComposeTask smsSender = new SmsComposeTask();
    smsSender.To = "99********";
    smsSender.Body = "Hey How are you!";
    smsSender.Show();
}

The Show method launches a composed message with the sender and body we have given. You need to click on the send icon to send the SMS.

SMSWinPhn3.gif

Up Next
    Ebook Download
    View all
    Learn
    View all