Example #1
0
        private async void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            ContactInformation contactInfo;

            if (!e.Results.Any())
            {
                // No contacts in the address book
                // Create demo contact
                contactInfo = new ContactInformation
                {
                    FamilyName = "Jakl",
                    GivenName  = "Andreas"
                };
                var contactProps = await contactInfo.GetPropertiesAsync();

                contactProps.Add(KnownContactProperties.CompanyName, "Mopius");
                contactProps.Add(KnownContactProperties.Url, "http://www.mopius.com/");
            }
            else
            {
                // Found a contact in the address book?
                // Use first contact from the address book
                var contact = e.Results.First();
                // Use library utility class to convert the Contact class from the WP address book
                // to the WP ContactInformation class that can convert to vCard format.
                contactInfo = await NdefVcardRecord.ConvertContactToInformation(contact);
            }

            // Create new NDEF record based on selected contact
            var vcardRecord = await NdefVcardRecord.CreateFromContactInformation(contactInfo);

            PublishRecord(vcardRecord, true);
        }
Example #2
0
 private async  void bt_add_Click(object sender, RoutedEventArgs e)
 {
     //创建一个系统通信可以读写和其他程序制度的联系人存储
     ContactStore conStore = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);
     //新增联系人
     ContactInformation conInfo = new ContactInformation();
     //获取ContacInformation类的属性map表
     var properties = await conInfo.GetPropertiesAsync();
     //添加电话属性
     properties.Add(KnownContactProperties.FamilyName, "test");
     properties.Add(KnownContactProperties.Telephone, "123456789");
  
     //创建联系人对象
     StoredContact storedContact = new StoredContact(conStore, conInfo);
     //获取安装包的一张图片文件用作联系人的头像
     StorageFile imagefile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Assets/1.jpeg");
         
     
     ///设置头像,将图片数据转化成Stream对象,在转化成IInputStream对象。
     //打开文件的可读数据流
     Stream stream = await imagefile.OpenStreamForReadAsync();
     //用Stream对象转化成为IIputStream对象
     IInputStream inputStream = stream.AsInputStream();
     //用IInputStream 对象设置为联系人头像
     await storedContact.SetDisplayPictureAsync(inputStream);
     //保存联系人
     await storedContact.SaveAsync();
     
     ///获取头像,接收到的图片数据为IRandomAccessStream类型,如果要展示出来,需要创建一个BitmapImage对象,
     IRandomAccessStream raStream = await storedContact.GetDisplayPictureAsync();
     BitmapImage bi = new BitmapImage();
     bi.SetSource(raStream);
     image.Source = bi;
 }
Example #3
0
 private async void Button_Click_1(object sender, RoutedEventArgs e)
 {
     ContactStore contactStore = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);
     ContactInformation contactInformation=new ContactInformation();
     var properties = await contactInformation.GetPropertiesAsync();
     properties.Add(KnownContactProperties.FamilyName, name.Text);
     properties.Add(KnownContactProperties.Telephone, tel.Text);
     
     StoredContact storedContact = new StoredContact(contactStore, contactInformation);
     if (stream != null)
     {
         await storedContact.SetDisplayPictureAsync(stream);
     }
     await storedContact.SaveAsync();
     NavigationService.GoBack();
 }
Example #4
0
        private async void bt_add_Click(object sender, RoutedEventArgs e)
        {
            //创建一个系统通信可以读写和其他程序制度的联系人存储
            ContactStore conStore = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);

            //新增联系人
            ContactInformation conInfo = new ContactInformation();
            //获取ContacInformation类的属性map表
            var properties = await conInfo.GetPropertiesAsync();

            //添加电话属性
            properties.Add(KnownContactProperties.FamilyName, "test");
            properties.Add(KnownContactProperties.Telephone, "123456789");

            //创建联系人对象
            StoredContact storedContact = new StoredContact(conStore, conInfo);
            //获取安装包的一张图片文件用作联系人的头像
            StorageFile imagefile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Assets/1.jpeg");


            ///设置头像,将图片数据转化成Stream对象,在转化成IInputStream对象。
            //打开文件的可读数据流
            Stream stream = await imagefile.OpenStreamForReadAsync();

            //用Stream对象转化成为IIputStream对象
            IInputStream inputStream = stream.AsInputStream();
            //用IInputStream 对象设置为联系人头像
            await storedContact.SetDisplayPictureAsync(inputStream);

            //保存联系人
            await storedContact.SaveAsync();

            ///获取头像,接收到的图片数据为IRandomAccessStream类型,如果要展示出来,需要创建一个BitmapImage对象,
            IRandomAccessStream raStream = await storedContact.GetDisplayPictureAsync();

            BitmapImage bi = new BitmapImage();

            bi.SetSource(raStream);
            image.Source = bi;
        }
Example #5
0
        private async void Save_Click(object sender, RoutedEventArgs e)
        {
            string message = "";

            if (name.Text != "" && telphone.Text != "")
            {
                try
                {
                    //创建一个联系人的信息对象
                    ContactInformation conInfo = new ContactInformation();
                    //获取联系人的属性字典
                    var properties = await conInfo.GetPropertiesAsync();

                    //添加联系人的属性
                    properties.Add(KnownContactProperties.GivenName, name.Text);
                    properties.Add(KnownContactProperties.Telephone, telphone.Text);
                    //创建或者打开联系人存储
                    ContactStore conStore = await ContactStore.CreateOrOpenAsync();

                    StoredContact storedContact = new StoredContact(conStore, conInfo);
                    //保存联系人
                    await storedContact.SaveAsync();

                    message = "保存成功";
                }
                catch (Exception ex)
                {
                    message = "保存失败,错误信息:" + ex.Message;
                }
            }
            else
            {
                message = "名字或电话不能为空";
            }
            await new MessageDialog(message).ShowAsync();
            (Window.Current.Content as Frame).Navigate(typeof(ContactsList));
        }
Example #6
0
        /// <summary>
        /// Called when reading a contact from the address book (Contact instance)
        /// and we need to convert it to a ContactInformation instance that
        /// supports conversion to a vCard.
        /// </summary>
        /// <remarks>
        /// Note that there is no 1:1 conversion possible between those two formats,
        /// as they have a very different structure. This method does its best to
        /// map and retain as much information as possible.
        ///
        /// Currently, this method is only working with KnownContactProperties of
        /// the ContactInformation object, and doesn't define custom properties.
        /// </remarks>
        /// <param name="contact">the contact from the address book to convert.</param>
        /// <returns>Converted contact information.</returns>
        public static async Task <ContactInformation> ConvertContactToInformation(Contact contact)
        {
            var contactInfo  = new ContactInformation();
            var contactProps = await contactInfo.GetPropertiesAsync();

            contactProps.Add(KnownContactProperties.DisplayName, contact.DisplayName);

            // CompleteName
            var completeName = contact.CompleteName;

            AddContactPropertyIfPossible(KnownContactProperties.GivenName, completeName.FirstName, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.FamilyName, completeName.LastName, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.AdditionalName, completeName.MiddleName, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.Nickname, completeName.Nickname, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.HonorificPrefix, completeName.Title, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.HonorificSuffix, completeName.Suffix, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.YomiFamilyName, completeName.YomiLastName, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.YomiGivenName, completeName.YomiFirstName, contactProps);

            // Addresses
            foreach (var curAddress in contact.Addresses)
            {
                switch (curAddress.Kind)
                {
                case AddressKind.Home:
                    AddAddressPropertyIfPossible(KnownContactProperties.Address, curAddress.PhysicalAddress, contactProps);
                    break;

                case AddressKind.Work:
                    AddAddressPropertyIfPossible(KnownContactProperties.WorkAddress, curAddress.PhysicalAddress, contactProps);
                    break;

                case AddressKind.Other:
                    AddAddressPropertyIfPossible(KnownContactProperties.OtherAddress, curAddress.PhysicalAddress, contactProps);
                    break;
                }
            }

            // Birthdays
            foreach (var curBirthday in contact.Birthdays)
            {
                AddContactPropertyIfPossible(KnownContactProperties.Birthdate, new DateTimeOffset(curBirthday), contactProps);
            }

            // Children
            AddStringContactProperties(contact.Children, KnownContactProperties.Children, contactProps);

            // Companies
            foreach (var curCompany in contact.Companies)
            {
                AddContactPropertyIfPossible(KnownContactProperties.CompanyName, curCompany.CompanyName, contactProps);
                AddContactPropertyIfPossible(KnownContactProperties.JobTitle, curCompany.JobTitle, contactProps);
                AddContactPropertyIfPossible(KnownContactProperties.OfficeLocation, curCompany.OfficeLocation, contactProps);
                AddContactPropertyIfPossible(KnownContactProperties.YomiCompanyName, curCompany.YomiCompanyName, contactProps);
            }

            // EmailAddresses
            foreach (var curEmail in contact.EmailAddresses)
            {
                switch (curEmail.Kind)
                {
                case EmailAddressKind.Personal:
                    AddContactPropertyIfPossible(KnownContactProperties.Email, curEmail.EmailAddress, contactProps);
                    break;

                case EmailAddressKind.Work:
                    AddContactPropertyIfPossible(KnownContactProperties.WorkEmail, curEmail.EmailAddress, contactProps);
                    break;

                case EmailAddressKind.Other:
                    AddContactPropertyIfPossible(KnownContactProperties.OtherEmail, curEmail.EmailAddress, contactProps);
                    break;
                }
            }

            // Notes
            AddStringContactProperties(contact.Notes, KnownContactProperties.Notes, contactProps);

            // PhoneNumbers
            foreach (var curNumber in contact.PhoneNumbers)
            {
                switch (curNumber.Kind)
                {
                case PhoneNumberKind.Mobile:
                    AddContactPropertyIfPossible(KnownContactProperties.MobileTelephone, curNumber.PhoneNumber, contactProps);
                    break;

                case PhoneNumberKind.Home:
                    AddContactPropertyIfPossible(KnownContactProperties.Telephone, curNumber.PhoneNumber, contactProps);
                    break;

                case PhoneNumberKind.Work:
                    AddContactPropertyIfPossible(KnownContactProperties.WorkTelephone, curNumber.PhoneNumber, contactProps);
                    break;

                case PhoneNumberKind.Company:
                    AddContactPropertyIfPossible(KnownContactProperties.CompanyTelephone, curNumber.PhoneNumber, contactProps);
                    break;

                case PhoneNumberKind.Pager:
                    // N/A
                    break;

                case PhoneNumberKind.HomeFax:
                    AddContactPropertyIfPossible(KnownContactProperties.HomeFax, curNumber.PhoneNumber, contactProps);
                    break;

                case PhoneNumberKind.WorkFax:
                    AddContactPropertyIfPossible(KnownContactProperties.WorkFax, curNumber.PhoneNumber, contactProps);
                    break;
                }
            }

            // SignificantOthers
            AddStringContactProperties(contact.SignificantOthers, KnownContactProperties.SignificantOther, contactProps);

            // Websites
            AddStringContactProperties(contact.Websites, KnownContactProperties.Url, contactProps);

            // Display name
            contactInfo.DisplayName = contact.DisplayName;

            return(contactInfo);
        }
Example #7
0
        /// <summary>
        /// Called when reading a contact from the address book (Contact instance) 
        /// and we need to convert it to a ContactInformation instance that
        /// supports conversion to a vCard.
        /// </summary>
        /// <remarks>
        /// Note that there is no 1:1 conversion possible between those two formats,
        /// as they have a very different structure. This method does its best to
        /// map and retain as much information as possible.
        /// 
        /// Currently, this method is only working with KnownContactProperties of
        /// the ContactInformation object, and doesn't define custom properties.
        /// </remarks>
        /// <param name="contact">the contact from the address book to convert.</param>
        /// <returns>Converted contact information.</returns>
        public static async Task<ContactInformation> ConvertContactToInformation(Contact contact)
        {
            var contactInfo = new ContactInformation();
            var contactProps = await contactInfo.GetPropertiesAsync();

            contactProps.Add(KnownContactProperties.DisplayName, contact.DisplayName);

            // CompleteName
            var completeName = contact.CompleteName;
            AddContactPropertyIfPossible(KnownContactProperties.GivenName, completeName.FirstName, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.FamilyName, completeName.LastName, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.AdditionalName, completeName.MiddleName, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.Nickname, completeName.Nickname, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.HonorificPrefix, completeName.Title, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.HonorificSuffix, completeName.Suffix, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.YomiFamilyName, completeName.YomiLastName, contactProps);
            AddContactPropertyIfPossible(KnownContactProperties.YomiGivenName, completeName.YomiFirstName, contactProps);

            // Addresses
            foreach (var curAddress in contact.Addresses)
            {
                switch (curAddress.Kind)
                {
                    case AddressKind.Home:
                        AddAddressPropertyIfPossible(KnownContactProperties.Address, curAddress.PhysicalAddress, contactProps);
                        break;
                    case AddressKind.Work:
                        AddAddressPropertyIfPossible(KnownContactProperties.WorkAddress, curAddress.PhysicalAddress, contactProps);
                        break;
                    case AddressKind.Other:
                        AddAddressPropertyIfPossible(KnownContactProperties.OtherAddress, curAddress.PhysicalAddress, contactProps);
                        break;
                }
            }

            // Birthdays
            foreach (var curBirthday in contact.Birthdays)
            {
                AddContactPropertyIfPossible(KnownContactProperties.Birthdate, new DateTimeOffset(curBirthday), contactProps);
            }

            // Children
            AddStringContactProperties(contact.Children, KnownContactProperties.Children, contactProps);

            // Companies
            foreach (var curCompany in contact.Companies)
            {
                AddContactPropertyIfPossible(KnownContactProperties.CompanyName, curCompany.CompanyName, contactProps);
                AddContactPropertyIfPossible(KnownContactProperties.JobTitle, curCompany.JobTitle, contactProps);
                AddContactPropertyIfPossible(KnownContactProperties.OfficeLocation, curCompany.OfficeLocation, contactProps);
                AddContactPropertyIfPossible(KnownContactProperties.YomiCompanyName, curCompany.YomiCompanyName, contactProps);
            }

            // EmailAddresses
            foreach (var curEmail in contact.EmailAddresses)
            {
                switch (curEmail.Kind)
                {
                    case EmailAddressKind.Personal:
                        AddContactPropertyIfPossible(KnownContactProperties.Email, curEmail.EmailAddress, contactProps);
                        break;
                    case EmailAddressKind.Work:
                        AddContactPropertyIfPossible(KnownContactProperties.WorkEmail, curEmail.EmailAddress, contactProps);
                        break;
                    case EmailAddressKind.Other:
                        AddContactPropertyIfPossible(KnownContactProperties.OtherEmail, curEmail.EmailAddress, contactProps);
                        break;
                }
            }

            // Notes
            AddStringContactProperties(contact.Notes, KnownContactProperties.Notes, contactProps);

            // PhoneNumbers
            foreach (var curNumber in contact.PhoneNumbers)
            {
                switch (curNumber.Kind)
                {
                    case PhoneNumberKind.Mobile:
                        AddContactPropertyIfPossible(KnownContactProperties.MobileTelephone, curNumber.PhoneNumber, contactProps);
                        break;
                    case PhoneNumberKind.Home:
                        AddContactPropertyIfPossible(KnownContactProperties.Telephone, curNumber.PhoneNumber, contactProps);
                        break;
                    case PhoneNumberKind.Work:
                        AddContactPropertyIfPossible(KnownContactProperties.WorkTelephone, curNumber.PhoneNumber, contactProps);
                        break;
                    case PhoneNumberKind.Company:
                        AddContactPropertyIfPossible(KnownContactProperties.CompanyTelephone, curNumber.PhoneNumber, contactProps);
                        break;
                    case PhoneNumberKind.Pager:
                        // N/A
                        break;
                    case PhoneNumberKind.HomeFax:
                        AddContactPropertyIfPossible(KnownContactProperties.HomeFax, curNumber.PhoneNumber, contactProps);
                        break;
                    case PhoneNumberKind.WorkFax:
                        AddContactPropertyIfPossible(KnownContactProperties.WorkFax, curNumber.PhoneNumber, contactProps);
                        break;
                }
            }

            // SignificantOthers
            AddStringContactProperties(contact.SignificantOthers, KnownContactProperties.SignificantOther, contactProps);

            // Websites
            AddStringContactProperties(contact.Websites, KnownContactProperties.Url, contactProps);

            // Display name
            contactInfo.DisplayName = contact.DisplayName;

            return contactInfo;

        }
Example #8
0
        private async void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            ContactInformation contactInfo;
            if (!e.Results.Any())
            {
                // No contacts in the address book
                // Create demo contact
                contactInfo = new ContactInformation
                {
                    FamilyName = "Jakl",
                    GivenName = "Andreas"
                };
                var contactProps = await contactInfo.GetPropertiesAsync();
                contactProps.Add(KnownContactProperties.Url, "http://www.nfcinteractor.com/");
            }
            else
            {
                // Found a contact in the address book?
                // Use first contact from the address book
                var contact = e.Results.First();
                // Use library utility class to convert the Contact class from the WP address book
                // to the WP ContactInformation class that can convert to vCard format.
                contactInfo = await NdefVcardRecord.ConvertContactToInformation(contact);
            }

            // Create new NDEF record based on selected contact
            var vcardRecord = await NdefVcardRecord.CreateFromContactInformation(contactInfo);

            PublishRecord(vcardRecord, true);
        }