ContactManager.RequestStoreAsync()抛出System.UnauthorizedAccessException

时间:2015-07-30 19:26:53

标签: c# windows windows-store-apps windows-10

我正在尝试在Windows 10 Universal apps API中使用ContactManager类。我试图在Windows 10桌面计算机上执行此操作。

我收到一个例外," System.UnauthorizedAccessException"尝试使用ContactManager.RequestStoreAsync()时请求联系人列表。

在以前的版本中,此功能仅适用于Windows Phone设备。微软文档说它现在需要一个Windows 10设备系列,但我没有运气。

using Windows.ApplicationModel.Contacts;

public async Task<List<String>> getContacts()
    {
        List<String> listResults = new List<string>();
        ContactStore store = null;
        IReadOnlyList<ContactList> list = null;
        ContactReader reader = null;
        ContactBatch batch = null;

        // *** This RequestStoreAsync() call is where the exception is thrown. All the cases below have the same issue. ***
        //store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadWrite);
        //store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite);
        store = await ContactManager.RequestStoreAsync();

        list = await store.FindContactListsAsync();
        foreach (ContactList contactList in list)
        {
            reader = contactList.GetContactReader();
            batch = await reader.ReadBatchAsync();
            foreach (Contact contact in batch.Contacts)
            {
                listResults.Add(contact.Name);
            }
        }


        return listResults;
    }

2 个答案:

答案 0 :(得分:7)

好吧,我想我自己找到了答案。看起来如果您添加&#34;联系人&#34;手动对Package.appxmanifest文件的功能,它将解决问题。

此功能没有UI选项。您必须以某种方式知道它存在,在文本编辑器中而不是在UI中编辑文件,并添加:

<uap:Capability Name="contacts" />

答案 1 :(得分:0)

自2018年起,至少在VS2017中,在可视化编辑 Package.appxmanifest 时,功能选项卡上列出了 此类功能。无论如何,

<uap:Capability Name="contacts" />

是清单所需的关键所在。

相关问题