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.
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 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.
8) Stop the execution and now add method “LoadSampleContacts()” into “MainPage.xaml.cs” file.
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:
That’s about it.