簡體   English   中英

ContactManager.RequestStoreAsync()拋出System.UnauthorizedAccessException

[英]ContactManager.RequestStoreAsync() throws System.UnauthorizedAccessException

我試圖在Windows 10 Universal apps API中使用ContactManager類。 我試圖在Windows 10桌面計算機上執行此操作。

我在嘗試使用ContactManager.RequestStoreAsync()請求聯系人列表時收到異常“System.UnauthorizedAccessException”。

在以前的版本中,此功能僅適用於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;
    }

好吧,我想我自己就找到了答案。 看起來如果您手動將“聯系人”功能添加到Package.appxmanifest文件,它將解決問題。

此功能沒有UI選項。 您必須以某種方式知道它存在,在文本編輯器中而不是在UI中編輯文件,並添加:

<uap:Capability Name="contacts" />

截至2018年, 功能選項卡上列出當目視編輯Package.appxmanifest,至少在VS2017這樣的能力。 無論如何,

<uap:Capability Name="contacts" />

是清單所需的關鍵事項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM