It’s getting rather cold in UK and I am dreading the winter and the darkness. However, on the positive side it is cozier to sit and code in front of the computer with a lovely cup of tea or coffee. And here is some code.

I was asked on Twitter to post about adding contacts for Windows Store and Windows Phone when working on a Universal App, and frankly contacts and the various APIs around that confuses me (so I hope I got it right). This is one of the areas where we don’t have full convergence yet between the two targeted devices, some types are available for one platform (such as the ContactManager class which at the time of typing is only available for Windows Store). To add to the confusion there is the concept of a contact store, an in-app contacts keeper we could call it, which is available on Windows Phone only. After reading the documentation up and down I ended up with the code below for adding contacts to the People app (or hub as it can also be called) for Windows Phone and Windows Store. Whatever you want to do outside of the app container has to be done through either special permissions (- and capabilities declared) when they exist, or through a broker model that basically hands over the decision making over to the user.

In the code example below the Store application creates a contact, then opens up a dialog with the details and the user can either take direct actions on the details (send an email for example), or add the contact (unless already added)- from where the user can find the contact details in the People hub/app.

For Windows Phone the contact is added directly and can afterwards be accessed in the People hub. The app, if you are curious, is the app I’ve used for the last few Optical Character recognition blog posts and it simply takes an image, grabs the text and layout information and with some regex and layout information trickery (logic) creates a contact. Don’t forget to add Contact as a capability for Windows Phone in the manifest file BTW!

[sourcecode language="csharp”]

if (Contact == null) return;

#if WINDOWS_APP

var contact = new Contact

{

FirstName = Contact.Name

};

if (!string.IsNullOrEmpty(Contact.Email)) { var homeEmail = new ContactEmail { Address = Contact.Email, Kind = ContactEmailKind.Work }; contact.Emails.Add(homeEmail); } if (!string.IsNullOrEmpty(Contact.PhoneNumber)) { var workPhone = new ContactPhone { Number = Contact.PhoneNumber, Kind = ContactPhoneKind.Work }; contact.Phones.Add(workPhone); } ContactManager.ShowContactCard(contact, new Rect(), Placement.Above);

#endif

#if WINDOWS_PHONE_APP

var contactStore = await Windows.Phone.PersonalInformation.ContactStore.CreateOrOpenAsync(

ContactStoreSystemAccessMode.ReadWrite,

ContactStoreApplicationAccessMode.ReadOnly);

var contact = new StoredContact(contactStore);

var contactDetails = await contact.GetPropertiesAsync();

if (!string.IsNullOrEmpty(Contact.Name)) { contactDetails.Add(KnownContactProperties.GivenName, Contact.Name); } if (!string.IsNullOrEmpty(Contact.PhoneNumber)) { contactDetails.Add(KnownContactProperties.WorkTelephone, Contact.PhoneNumber); } if (!string.IsNullOrEmpty(Contact.PhoneNumber)) { contactDetails.Add(KnownContactProperties.Email, Contact.Email); } await contact.SaveAsync();

#endif

[/sourcecode]

For Windows Store there is also the CurrentPickerUI which (when used with a Contact contract) lets the user use your app to select contacts in a similar fashion as the Share Contract target and source works.

Alright, let me know if I’ve missed something here or something, I can’t wait until Windows Store and Windows Phone become one and the APIs are a bit clearer in the way they work and what they do. Still love it though, the platform.

Happy coding!

Last modified on 2014-12-08