在我的应用程序中,我使用StoredContactContactStore创建联系人,并通过KnwonContactProperties.MobileTelephone使用GetPropertiesAsync设置手机号码。

很好,我可以在“人脉”中看到手机号码。

但...

如果我尝试通过ContactManager.RequestStoreAsync编程访问联系人,则在contact.Phones集合中看不到该电话号码。

有什么方法可以将电话号码写入电话收藏集吗?

(Related question)

最佳答案

KnownContactProperties类在Windows.Phone.PhoneContract命名空间下,但ContactManager.RequestStoreAsync()在Windows.ApplicationModel.Contacts命名空间下。这可能是您无法获取电话号码的原因。在Windows.Phone.PhoneContract下的ContactStore.CreateOrOpenAsync方法与KnownContactProperties相同,可以很好地工作。这是一个完整的演示,用于插入联系人,然后获取联系人的姓名和电话号码。

XAML代码

<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>

后面的代码
 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());
     }
 }

关于c# - 如何写到ContactStore.Contact.Phones?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39227004/

10-11 11:15