Windows Phone 7 SDK exposes API to provide user option to save any contact to the Contact list. In your app, you may want to include contact saving option. This small post will help you to understand the API and after that, you will be able to use it in any of your applications.
The SDK has a sealed
class called SaveContactTask
, which you can use to implement this feature. Continue reading to know more about the class and implementation steps.
Know About the API
SaveContactTask
is a sealed
class present in the Microsoft.Phone.Tasks
namespace which derives from ChooserBase
of type SaveContactResult
. The SaveContactTask
class provides a bunch of properties, which you can set to auto fill the contact information. The class has only one method called Show()
which launches the New Contact screen.
Here is the meta data of the SaveContactTask
class, where you can see all the exposed properties that you can use:
namespace Microsoft.Phone.Tasks
{
public sealed class SaveContactTask : ChooserBase<savecontactresult>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string Nickname { get; set; }
public string Suffix { get; set; }
public string Company { get; set; }
public string Title { get; set; }
public string MobilePhone { get; set; }
public string HomePhone { get; set; }
public string WorkPhone { get; set; }
public string PersonalEmail { get; set; }
public string WorkEmail { get; set; }
public string OtherEmail { get; set; }
public string HomeAddressStreet { get; set; }
public string HomeAddressCity { get; set; }
public string HomeAddressState { get; set; }
public string HomeAddressZipCode { get; set; }
public string HomeAddressCountry { get; set; }
public string WorkAddressStreet { get; set; }
public string WorkAddressCity { get; set; }
public string WorkAddressState { get; set; }
public string WorkAddressZipCode { get; set; }
public string WorkAddressCountry { get; set; }
public string Website { get; set; }
public string Notes { get; set; }
public string JobTitle { get; set; }
public override void Show();
}
}
You can use the Completed
event to know whether the saving operation has been successfully ended or not.
Implementation Steps
To implement the contact saving operation, create the instance of the class and set the optional properties that you want to auto fill when user launches the save contact screen from your application. Register for the completed event to know whether the saving operation has been successful.
var saveContactTask = new SaveContactTask();
saveContactTask.Title = "Mr.";
saveContactTask.FirstName = "Kunal";
saveContactTask.LastName = "Chowdhury";
saveContactTask.MobilePhone = "+91-8888779569";
saveContactTask.HomePhone = "(018) 741236555";
saveContactTask.WorkPhone = "(018) 1999984562";
saveContactTask.Website = "www.kunal-chowdhury.com";
saveContactTask.Completed += SaveContactTask_Completed;
saveContactTask.Show();
Finally, call the Show()
method to launch the New contact saving screen in your Windows Phone 7 device. Based on the SaveContactResult
in the Completed
event implementation, take necessary actions as demonstrated below:
void SaveContactTask_Completed(object sender, SaveContactResult e)
{
switch (e.TaskResult)
{
case TaskResult.OK:
break;
case TaskResult.Cancel:
break;
default:
break;
}
}
That’s all about the coding part. When you launch it, you will see that the new phone contact screen has been auto populated with the values that you set from the code. If you want to add more details, you will be able to do that from that screen as shown below:
Finally, the user needs to Save the contact by clicking the save button present in the App Bar of the screen, else the contact will not getting saved automatically. Hope this small tip was helpful for you to understand the API and the implementation process.