private async void ContactPicker_Click(object
sender, RoutedEventArgs e)
{
ContactPicker contactPicker = new ContactPicker();
contactPicker.CommitButtonText = "Select";
contactPicker.SelectionMode = ContactSelectionMode.Fields;
contactPicker.DesiredFields.Add(KnownContactField.Email);
ContactInformation contact = await
contactPicker.PickSingleContactAsync();
if
(contact != null)
{
this.OutputName.Text =
contact.Name;
AppendContactFields(this.OutputEmails, contact.Emails);
AppendContactFields(this.OutputAddresses, contact.Locations);
IRandomAccessStreamWithContentType stream = await
contact.GetThumbnailAsync();
if (stream != null
&& stream.Size >
0)
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(stream);
this.OutputThumbnail.Source = bitmap;
}
}
}
private static void
AppendContactFields(TextBlock content, IReadOnlyCollection
fields)
{
if
(fields.Count > 0)
{
StringBuilder output = new
StringBuilder();
foreach (IContactField field
in fields)
{
if (field.Type ==
ContactFieldType.Location)
AppendLocationValue(output, (ContactLocationField)field);
else
output.AppendFormat("{0} ({1})\n", field.Value,
field.Category);
}
content.Text =
output.ToString();
}
}
private static void
AppendLocationValue(StringBuilder output, ContactLocationField
location)
{
string
address = location.UnstructuredAddress;
if
(string.IsNullOrEmpty(address))
{
List parts = (new List {
location.Street, location.City, location.Region, location.Country,
location.PostalCode });
address = string.Join(" ; ",
parts.FindAll(s => !string.IsNullOrEmpty(s)));
}
output.AppendFormat("{0} ({1})\n", address,
location.Category);
}
//多个联系人
public
IReadOnlyList<ContactInformation>
contacts;
private async void ContactPicker_Click(object
sender, RoutedEventArgs e)
{
ContactPicker contactPicker = new ContactPicker();
contactPicker.CommitButtonText = "Select";
contactPicker.SelectionMode = ContactSelectionMode.Fields;
contactPicker.DesiredFields.Add(KnownContactField.Email);
contacts =
await contactPicker.PickMultipleContactsAsync();
if
(contacts.Count > 0)
{
foreach (ContactInformation
contact in contacts)
{
// Add the contacts to your
wanted
string contactName = contact.Name;
string contactEmail=null;
string contactLocation = null;
string contactPhoneNumnber = null;
if (contact.Emails.Count >
0)
contactEmail = contact.Emails[0].Value;
else if (contact.PhoneNumbers.Count
> 0)
contactPhoneNumnber = contact.PhoneNumbers[0].Value;
else if (contact.Locations.Count
> 0)
contactLocation = contact.Locations[0].UnstructuredAddress;
IRandomAccessStreamWithContentType stream =
await contact.GetThumbnailAsync();
if (stream != null
&& stream.Size >
0)
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
this.OutputThumbnail.Source = bitmapImage;
}
}
}
}