Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write to ContactStore.Contact.Phones?

From my app I create contacts using StoredContact and the ContactStore, setting the mobile phone number using KnwonContactProperties.MobileTelephone via GetPropertiesAsync.

This is fine, and I can see the mobile phone number in People.

But...

If I try to access the contacts programatically via ContactManager.RequestStoreAsync, I don't see this phone number in the contact.Phones collection.

Is there any way to get numbers written into the Phones collection?

(Related question)

like image 382
Benjol Avatar asked Aug 30 '16 12:08

Benjol


1 Answers

The KnownContactProperties class is in under Windows.Phone.PhoneContractnamespace, but ContactManager.RequestStoreAsync() is under Windows.ApplicationModel.Contacts namespace. It may be the reason you cannot get the phone numbers. ContactStore.CreateOrOpenAsync method under Windows.Phone.PhoneContract same with KnownContactProperties can work well. Here is a completed demo for inserting a contact and then get the contact's name and phone number.

XAML Code

<StackPanel>
    <TextBox x:Name="txtName" Header="name" InputScope="NameOrPhoneNumber"/>
    <TextBox x:Name="txtTel" Header="phone number 1" InputScope="ChineseHalfWidth"/>
    <TextBox x:Name="txtTel1" Header="phone number 2" InputScope="TelephoneNumber"/>
    <Button x:Name="btnSave" Content="Save" Click="btnSave_Click"/>
    <Button x:Name="btnGet" Content="GET" Click="btnGet_Click"/> 
</StackPanel>

Code behind

 private async void btnSave_Click(object sender, RoutedEventArgs e)
 {
     var name = txtName.Text;
     var tel = txtTel.Text;

     ContactStore contactStore = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);
     ContactInformation contactInformation = new ContactInformation();
     contactInformation.DisplayName = name;
     var contactProps = await contactInformation.GetPropertiesAsync();
     contactProps.Add(KnownContactProperties.MobileTelephone, tel);
     StoredContact storedContact = new StoredContact(contactStore, contactInformation);
     await storedContact.SaveAsync();
 }

 private async void btnGet_Click(object sender, RoutedEventArgs e)
 {

     ContactStore contactStore = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);
     var result = contactStore.CreateContactQuery();
     var count = await result.GetContactCountAsync();
     var list = await result.GetContactsAsync();
     foreach (var item in list)
     {
         var properties = await item.GetPropertiesAsync();
         System.Diagnostics.Debug.WriteLine(item.DisplayName);                
         System.Diagnostics.Debug.WriteLine(properties[KnownContactProperties.MobileTelephone].ToString());
     }
 }
like image 169
Sunteen Wu Avatar answered Sep 20 '22 10:09

Sunteen Wu