简体   繁体   中英

ContactManager.RequestStoreAsync() throws System.UnauthorizedAccessException

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM