ContactManager.RequestStoreAsync() throws System.UnauthorizedAccessException

核能气质少年 提交于 2019-11-28 10:37:59

问题


I am trying to use the ContactManager class in the Windows 10 Universal apps API. I am trying to do this on a Windows 10 Desktop machine.

I am receiving an exception, "System.UnauthorizedAccessException" when trying to request a list of contacts using ContactManager.RequestStoreAsync().

In previous versions, this function only worked on Windows Phone devices. The Microsoft documentation just says it requires a Windows 10 Device family now, but I'm not having any luck.

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

回答1:


Alright, I think I discovered the answer on my own. Looks like if you add the "contacts" capability to the Package.appxmanifest file manually, it will fix the issue.

There is no UI option for this capability. You have to somehow know it exists, edit the file in a text editor instead of in the UI, and add:

<uap:Capability Name="contacts" />



回答2:


As of 2018, there is such capability listed on Capabilities tab when visually editing the Package.appxmanifest, at least in VS2017. Anyway, the

<uap:Capability Name="contacts" />

is the crucial thing required in manifest.



来源:https://stackoverflow.com/questions/31732418/contactmanager-requeststoreasync-throws-system-unauthorizedaccessexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!