分享

Metro_Dev_Center_ContactPicker

 牛人的尾巴 2015-12-01

Metro_Dev_Center_ContactPicker

(2013-01-24 11:30:38)
标签:

杂谈

//一个联系人
        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;
                    }

                }
            }
        }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多