Partager via


Loading Sample Contacts Data on Windows Phone 8 (WP8) Emulator

Today, I shall discuss about loading of sample contacts data on windows phone 8 emulator for testing purpose. Following are some prerequisites before you proceed any further in this tutorial.

Prerequisites:
1) Knowledge about Windows Phone platform.
2) Knowledge about Windows Phone Emulator.
2) Knowledge about Isolated Storage.

Unlike windows phone 7 emulator that provides pre-loaded contacts lists for testing purpose, windows phone 8 emulator does not provide any such pre-loading contacts lists. Windows Phone SDK 8 provides a feature of "Custom Contact Storage" which is similar to the concept of Isolated Storage but for only contacts storage. Security of manipulation of existing contacts without user concern through application was another reason for Custom Contact Storage feature. You can read further about this here. In this tutorial, what I am doing is simply loading sample contacts which are in vCard 3.0 format into my windows phone 8 emulator, in order to test contacts base applications on windows phone 8 emulator as well.

You can download the complete source code for this tutorial from here or you can follow step by step discussion below. The sample code is in Microsoft Visual Studio 2012.

Let us begin now.
1) Create a windows phone application with target of SDK 8 in your VS 2012 and name it "ContactsInWP8Emulator".
2) Create new folder inside your project and name it "vCards".
3) Add your sample " .vcf" files to that folder and add them in your VS project as well.
4) For simplicity, in the provided sample, I have added a "files.txt" file that simply contains the path of my contacts " .vcf" files.
5) From inside VS select these newly added files and make sure that from properties panel, "Build Action property is set to content" & "Copy if new" property is selected, since we wanted to movw these files to our " .xap" package.
6) Build and execute the project and after launching the application go to "People" application on WP8 emulator and you will see following empty phone book.
 

7) From your "WMAppManifest.xml" you also need to enable "ID_CAP_CONTACTS" capability.

8) Stop the execution and now add method "LoadSampleContacts() " into "MainPage.xaml.cs" file.

 private async void LoadSampleContacts() 
 { 
 try 
 { 
 // Loading vcards. 
 var filePath = App.GetResourceStream(new Uri("vCards/files.txt", UriKind.Relative)); 
 StreamReader sr1 = new StreamReader(filePath.Stream); 
 string cardInfo = string.Empty;
 
 // Loading. 
 while ((cardInfo = sr1.ReadLine()) != null) 
 { 
 // Getting contact details from .vcf. 
 var card = App.GetResourceStream(new Uri(cardInfo, UriKind.Relative)); 
 StreamReader sr2 = new StreamReader(card.Stream); 
 string cardDataString = sr2.ReadToEnd();
 
 // Converting vcard data string to bytes for parsing purpose by ContactInformation class. 
 MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(cardDataString)); 
 IInputStream input = WindowsRuntimeStreamExtensions.AsInputStream(ms); 
 
 // Creating contact store. 
 var store = await ContactStore.CreateOrOpenAsync(); 
 
 // Parsing vcard. 
 ContactInformation vcardInfo = await ContactInformation.ParseVcardAsync(input);
 
 // Creating phone contacts. 
 var phoneContact = new StoredContact(store, vcardInfo); 
 
 // Saving Contacts to contact store. 
 await phoneContact.SaveAsync(); 
 } 
 } 
 catch (Exception ex) 
 { 
 // information. 
 MessageBox.Show("Something goes wrong, please try again later", "Error", MessageBoxButton.OK); 
 Console.WriteLine(ex); 
 } 
 }
 The above method is quite self explanatory. It is first loading contacts ( .vcf) file path from "files.txt" file one by one, then it loads the contact card information from " .vcf" file and convert it into stream which is parse able by "ContactInformation" class. The function then either create or open already created Custom Contact Storage. The ContactInformation class will parse the contact card stream and import contact information from provided " .vcf" file. At the end the contact information is created and saved into the Custom Contact Storage and we are done.
 
 9)  Call "LoadSampleContacts() " method in MainPage constructor after "InitializeComponent() " method.
 
 10)  In the MainPage.xaml simply add some text to exit the application after its launch i.e.
 11)  After closing the application you will see that your application is installed on the emulator. Do not uninstall because if you uninstall the application then the Custom Contact Storage will also get removed because our application has created it and it is associated to our application.
 12)  Now, open the "Phone" application from inside of the windows phone 8 emulator and you will see:

I have taken the sample contacts " .vcf" file from QT-Mobility Demo and then separate them because ContactInformation class can parse one " .vcf" at a time.

That's about it.

Enjoy!! Coding.

Important :   This blog post is written by Asma Khalid, a bright student from MIC Lahore, you can find other useful posts on her personal blog at 
https://asmak9.blogspot.com/2013/10/loading-sample-contacts-data-on-windows.html

 

ContactsInWP8Emulator.zip

Comments

  • Anonymous
    November 01, 2013
    You can sign in on the emulator with your Microsoft account and your contacts will appear in the emulator.

  • Anonymous
    November 29, 2013
    i am new to the windows phone development .I have contacts list in my windows application, how to give search button and functionality  for those contacts. please give me functionality or sample for this. thanks . my mail id : mail2mallig@gmail.com

  • Anonymous
    December 22, 2013
    Dear arjun, the solution is attached with the post

  • Anonymous
    March 06, 2014
    Awesome post. Its really helpful. Thank you Shahid :)

  • Anonymous
    May 26, 2014
    Very good post. Thank you so musch

  • Anonymous
    September 18, 2014
    i took contacts from my mail but i con't fetch contacts at a time  for that help me