Save and Read Contacts in Windows Phone 7

In this article we are going to explore how to save and read contacts in Windows Phone 7.


Introduction

In this article we are going to explore how to save and read contacts in Windows Phone 7. In subsequent details we will learn how to save and display contacts in Windows Phone 7. In this article we will demonstrate how to do this in Windows Phone 7. Further let me explain there are two chooser tasks which are used to complete both tasks named SaveContactTask and AddressChooserTask; these two tasks are responsible for doing such a task. SaveContactTask is a sealed class with default properties which we have to implement. The SaveContactsTask and AddressChooserTask classes are also sealed classes with some properties. So to do all that use the steps shown below.

Step 1: In this step first of all we have to create a Windows Phone application; let's see how to create it.

  • Go to Visual Studio 2010
  • File->New->Project
  • Select the template named as Silverlight for Windows Phone
  • Select the Windows Phone application
  • Give it a name as you want.   

Step_1_1fig.jpg

Step_1_2fig.jpg

Step 2: In this step we will see the code for the MainPage.xaml.cs file which is shown 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 Mynewapp

{

    public partial class MainPage : PhoneApplicationPage

    {

        SaveContactTask MyConatct_Task = new SaveContactTask();

        AddressChooserTask MyAddress_Task = new AddressChooserTask();

        // Constructor

        public MainPage()

        {

            InitializeComponent();

            textBox1.Text = "";

            textBox2.Text = "";

            textBox3.Text = "";

            textBox4.Text = "";

            textBox5.Text = "";

            this.MyConatct_Task.Completed += new EventHandler<SaveContactResult>(MyConatct_Task_Completed);

            this.MyAddress_Task.Completed+=new EventHandler<AddressResult>(MyAddress_Task_Completed);

        }

        void MyConatct_Task_Completed(object sender, SaveContactResult e)

        {

            switch (e.TaskResult)

            {

                case TaskResult.OK:

                    MessageBox.Show("Your Contact is successfully saved.");

                    break;

                case TaskResult.Cancel:

                    MessageBox.Show("The user canceled the task.");

                    break;

                default:

                    break;

            }

        }

        void MyAddress_Task_Completed(object sender, AddressResult e)

        {

            switch (e.TaskResult)

            {

                case TaskResult.OK:

                    // Fetch the selected Contact

                    MessageBox.Show(e.DisplayName + "\n\n" + e.Address);

                    break;

                case TaskResult.Cancel:

                    // Contact Selection Operation Cancelled

                    MessageBox.Show("Address Chooser Task interrupted by the user");

                    break;

                default:

                    break;

            }

        }

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            // Set the optional properties that you want to auto populate with

            MyConatct_Task.FirstName = textBox1.Text;

            MyConatct_Task.LastName = textBox2.Text;

            MyConatct_Task.Company = textBox3.Text;

            MyConatct_Task.JobTitle = textBox4.Text;

            MyConatct_Task.WorkAddressCity = textBox5.Text;

            //MyConatct_Task.Completed += MyConatct_Task_Completed;

            // Call the Show method to open the Contact Screen

            MyConatct_Task.Show();

        }
        private void button2_Click(object sender, RoutedEventArgs e)

        {

            MyAddress_Task.Show();

        }
    }
}

 

Step 3: In this step we will see the code for the Mainpage.xaml file which is shown below.

 

Code:

 

<phone:PhoneApplicationPage

    x:Class="Mynewapp.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"

    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock x:Name="PageTitle" Text="Saving Contacts Demo" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontFamily="Comic Sans MS" FontSize="40" />

        </StackPanel>

        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <Grid.Background>

                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                    <GradientStop Color="Black" Offset="0" />

                    <GradientStop Color="#FFFFCEC7" Offset="1" />

                </LinearGradientBrush>

            </Grid.Background>

            <TextBox Name="textBox1" Text="F_Name" Margin="160,6,25,0" Height="76" VerticalAlignment="Top" />

            <TextBox Height="72" Margin="160,66,0,536" Name="textBox2" Text="L_Name" HorizontalAlignment="Left" Width="271" />

            <TextBox Height="72" Margin="160,0,25,480" Name="textBox3" Text="Company" VerticalAlignment="Bottom" />

            <TextBox Height="72" Margin="160,182,25,420" Name="textBox4" Text="Title" />

            <TextBox Height="72" Margin="160,241,25,361" Name="textBox5" Text="Address" />

            <Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />

            <Button Content="Show" Height="72" HorizontalAlignment="Left" Margin="10,90,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />

        </Grid>

    </Grid>

</phone:PhoneApplicationPage>


Step 4: In this step we will see the Design of the MainPage.xaml file which is shown below.

Designimg.jpg

Step 5: In this step we are going to run the application by pressing F5 and the output regarding this given below.

Output 1: In this output we will enter the contacts details of the person as shown below.

Output1new.jpg

Output 2: In this step whenever you click on the save button then you will see that figure shown below.

Output2new.jpg

Output 3: After clicking on the Save Icon below the image then you will see a popup message box which shows you that contact has been added successfully.

Output3new.jpg

Output 4: Whenever you click on the show button then it will display for you all the contacts inside the Phone which is like as shown below.

Outputlast.jpg

Here are some other useful resources which may help you.

Using Isolated Storage to Save And Get an Image in Windows Phone 7
How to Add a New Contact, Make a Call and Send SMS in Windows Phone 7
Isolated Storage to Save and Read MP3 Files From Windows Phone 7
Read Local XML File in Windows Phone 7
File Handling in Windows Phone 7