Windows Phone 8 allows developers to create their own m contact store. After the application adds a new contact through the application, the contact appears in Windows Phone hub and coexist with the user's system contact. Developers can create standard attributes for contacts, such as phone numbers and names, or store custom attributes. Developers can also use the contact storage API to synchronize the contact list of their users to the cloud.
Create a contact:
private async void Button_Click_4 (object sender, RoutedEventArgs e)
{
// var store = await ContactStore.CreateOrOpenAsync (ContactStoreSystemAccessMode.ReadOnly,
// ContactStoreApplicationAccessMode.LimitedReadOnly);
// link and open the contact, you need to add <Capability Name = "ID_CAP_CONTACTS" /> ability
var store = await ContactStore.CreateOrOpenAsync ();
Debug.WriteLine ("RevisionNumber:" + store.RevisionNumber);
StoredContact sc = new StoredContact (store);
sc.DisplayName = "Mark";
sc.HonorificPrefix = "et";
await sc.SaveAsync ();
}
Read contacts
private async void Button_Click_2 (object sender, RoutedEventArgs e)
{
// var store = await ContactStore.CreateOrOpenAsync (ContactStoreSystemAccessMode.ReadOnly,
// ContactStoreApplicationAccessMode.LimitedReadOnly);
// link and open the contact, you need to add <Capability Name = "ID_CAP_CONTACTS" /> ability
var store = await ContactStore.CreateOrOpenAsync ();
Debug.WriteLine ("RevisionNumber:" + store.RevisionNumber);
// Create contact query
ContactQueryResult result = store.CreateContactQuery ();
var count = await result.GetContactCountAsync ();
Debug.WriteLine ("GetContactCountAsync:" + count);
ContactQueryOptions option = result.GetCurrentQueryOptions ();
foreach (string filed in option.DesiredFields)
{
Debug.WriteLine ("filed:" + filed);
}
// Get contact list
var contacts = await result.GetContactsAsync ();
foreach (StoredContact contact in contacts)
{
Debug.WriteLine ("DisplayName:" + contact.DisplayName);
// Convert contact data into a VCard file
var vCard = await contact.ToVcardAsync ();
IInputStream inputStream = vCard.GetInputStreamAt (0);
ulong length = vCard.Size;
try
{
// Output the vCard file stream as a string
var readBuf = new Windows.Storage.Streams.Buffer ((uint) length);
var vCardOp = vCard.GetInputStreamAt (0) .ReadAsync (readBuf, (uint) length, InputStreamOptions.Partial);
vCardOp.Completed = (IAsyncOperationWithProgress <IBuffer, uint> asyncAction, AsyncStatus asyncStatus) =>
{
switch (asyncStatus)
{
case AsyncStatus.Completed:
Debug.WriteLine ("vCardString:" + MainPage.BufferToString (readBuf));
break;
case AsyncStatus.Error:
break;
case AsyncStatus.Canceled:
// Read is not cancelled in this sample.
break;
}
};
}
catch (Exception exp)
{
Debug.WriteLine (exp.ToString ());
}
}
}