Dec 082014
 
 December 8, 2014  Posted by at 4:44 pm C# & F#, Optical Character Recognition, Universal Apps, WinRT, WP  Add comments

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!

Adding contacts in a Universal Store App (Windows Store and Windows Phone)

Adding contacts in a Universal Store App (Windows Store and Windows Phone)

 


Adding contacts in a Universal Store App (Windows Store and Windows Phone)

Adding contacts in a Universal Store App (Windows Store and Windows Phone)

 

[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!

 

  One Response to “Adding contacts in a Universal Store App (Windows Store and Windows Phone)”

  1. I love how neat, and standards compliant!, your code is. Laid out so that readability is not compromised and no excess code or comments.
    Your design skill for the UI of your apps is smooth, simple and functional; all the best points!

    I’m really enjoying these posts about Universal Apps, and OCR in particular, and am looking forward to using your experience published in these posts to help me with an app that is bubbling in my head.

    Thanks Iris!!!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

What is 15 + 13 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)