Esempio n. 1
0
        /// <summary>
        /// Creates a contact store and add contacts.
        /// </summary>
        public static async void CreateContactStore()
        {
            ContactStore contactStore = await ContactStore.CreateOrOpenAsync(
                ContactStoreSystemAccessMode.ReadWrite,
                ContactStoreApplicationAccessMode.ReadOnly);

            foreach (SampleContact sampleContact in SampleContact.CreateSampleContacts())
            {
                StoredContact contact = new StoredContact(contactStore);
                IDictionary <string, object> props = await contact.GetPropertiesAsync();

                if (!string.IsNullOrEmpty(sampleContact.FirstName))
                {
                    props.Add(KnownContactProperties.GivenName, sampleContact.FirstName);
                }

                if (!string.IsNullOrEmpty(sampleContact.LastName))
                {
                    props.Add(KnownContactProperties.FamilyName, sampleContact.LastName);
                }

                if (!string.IsNullOrEmpty(sampleContact.HomeEmail))
                {
                    props.Add(KnownContactProperties.Email, sampleContact.HomeEmail);
                }

                if (!string.IsNullOrEmpty(sampleContact.WorkEmail))
                {
                    props.Add(KnownContactProperties.WorkEmail, sampleContact.WorkEmail);
                }

                if (!string.IsNullOrEmpty(sampleContact.HomePhone))
                {
                    props.Add(KnownContactProperties.Telephone, sampleContact.HomePhone);
                }

                if (!string.IsNullOrEmpty(sampleContact.WorkPhone))
                {
                    props.Add(KnownContactProperties.CompanyTelephone, sampleContact.WorkPhone);
                }

                if (!string.IsNullOrEmpty(sampleContact.MobilePhone))
                {
                    props.Add(KnownContactProperties.MobileTelephone, sampleContact.MobilePhone);
                }

                await contact.SaveAsync();
            }
        }
        /// <summary>
        /// Adds a contact to ContactPickerUI.
        /// </summary>
        /// <param name="sampleContact">Sample contact to add</param>
        private void AddSampleContact(SampleContact sampleContact)
        {
            Contact contact = new Contact();
            contact.Id = sampleContact.Id;
            contact.FirstName = sampleContact.FirstName;
            contact.LastName = sampleContact.LastName;

            if (!string.IsNullOrEmpty(sampleContact.HomeEmail))
            {
                ContactEmail homeEmail = new ContactEmail();
                homeEmail.Address = sampleContact.HomeEmail;
                homeEmail.Kind = ContactEmailKind.Personal;
                contact.Emails.Add(homeEmail);
            }

            if (!string.IsNullOrEmpty(sampleContact.WorkEmail))
            {
                ContactEmail workEmail = new ContactEmail();
                workEmail.Address = sampleContact.WorkEmail;
                workEmail.Kind = ContactEmailKind.Work;
                contact.Emails.Add(workEmail);
            }

            if (!string.IsNullOrEmpty(sampleContact.HomePhone))
            {
                ContactPhone homePhone = new ContactPhone();
                homePhone.Number = sampleContact.HomePhone;
                homePhone.Kind = ContactPhoneKind.Home;
                contact.Phones.Add(homePhone);
            }

            if (!string.IsNullOrEmpty(sampleContact.MobilePhone))
            {
                ContactPhone mobilePhone = new ContactPhone();
                mobilePhone.Number = sampleContact.MobilePhone;
                mobilePhone.Kind = ContactPhoneKind.Mobile;
                contact.Phones.Add(mobilePhone);
            }

            if (!string.IsNullOrEmpty(sampleContact.WorkPhone))
            {
                ContactPhone workPhone = new ContactPhone();
                workPhone.Number = sampleContact.WorkPhone;
                workPhone.Kind = ContactPhoneKind.Work;
                contact.Phones.Add(workPhone);
            }

            switch (this.contactPickerUI.AddContact(contact))
            {
                case AddContactResult.Added:
                    // Notify the user that the contact was added
                    OutputText.Text = contact.DisplayName + " was added to the basket";
                    break;
                case AddContactResult.AlreadyAdded:
                    // Notify the user that the contact is already added
                    OutputText.Text = contact.DisplayName + " is already in the basket";
                    break;
                case AddContactResult.Unavailable:
                default:
                    // Notify the user that the basket is unavailable
                    OutputText.Text = contact.DisplayName + " could not be added to the basket";
                    break;
            }
        }