In this tutorial we are going to see how to use the Save Contact task with Windows Phone Application development. In our earlier tutorials we have seen how to use the Address task and Email Address task to get the information of the contact, with this task we can create a contact from the application which we developed. Basically this task gets the information from the user about the contact and triggers the Contact Application to save the contact information to the contact list. Let us see the steps on how to achieve this task in real time for a Windows phone application. [more]
Open Visual Studio 2010 IDE and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below. Once the project is created add some controls which are used to trigger the Save Contact Chooser Task as shown in the screen below.
Now drag and drop few controls to the screen which is used to trigger event that launches the Email Save Contact Task. Once we designed the screen with the controls we can see the screen looks like below. We have added a button control to trigger the event and text block to show the end result after the task is completed as shown in the screen below.
Now we need to write our code in the button click event to trigger the Save Contact task on the user click to save the details to the contacts list. To do that just go to the code behind and first add the USING handler code on top with the existing using statements as shown in the code below.
using Microsoft.Phone.Tasks;
Next is to add the below code to the button click event which basically creates an instance of the Save Contact Chooser task and saves the details of the contact using the Contact application that is opened while the save contact task triggers as shown in the code below.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
namespace F5debugHowto23
{
public partial class MainPage : PhoneApplicationPage
{
SaveContactTask scTask;
// Constructor
public MainPage()
{
InitializeComponent();
scTask = new SaveContactTask();
scTask.Completed += new EventHandler<SaveContactResult>(scTask_Completed);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
scTask.FirstName = "F5debug";
scTask.LastName = "Karthik";
scTask.MobilePhone = "0123456789";
scTask.WorkEmail = "Karthikeyan@f5debug.net";
scTask.Show();
}
void scTask_Completed(object sender, SaveContactResult e)
{
switch (e.TaskResult)
{
case TaskResult.OK:
textBlock1.Text = "F5debug Contact Saved Successfully";
break;
case TaskResult.None:
textBlock1.Text = "F5debug Contact could not be Saved";
break;
case TaskResult.Cancel:
textBlock1.Text = "F5debug Contact Saved Cancelled";
break;
}
}
}
}
We have many properties that are available for the Save Contact task as shown in the screen below. We can use each of the properties with the appropriate values so that it saves most of the things to the contact.
Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.
Output Screens:
We can effectively use the available properties which can make this task more effectively used. Once its saved we can look into the contact list where our saved contact will be listed. That’s it from this tutorial on Windows Phone see you all in the next tutorial soon. Mean while Happy Programming!!!