c# - ContactManager.RequestStoreAsync() 抛出 System.UnauthorizedAccessException

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

我正在尝试使用 Windows 10 通用应用程序 API 中的 ContactManager 类。我正在尝试在 Windows 10 桌面计算机上执行此操作。

我在尝试使用 ContactManager.RequestStoreAsync() 请求联系人列表时收到异常“System.UnauthorizedAccessException”。

在以前的版本中,此功能仅适用于 Windows Phone 设备。 Microsoft 文档只是说它现在需要 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" />

关于c# - ContactManager.RequestStoreAsync() 抛出 System.UnauthorizedAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31732418/

相关文章:

c# - DevExpress GridControl 行号

c# - 将 GraphQL 从 .NET core 2.2 升级到 3.0

c# - 如何在 net-core 2.0 中手动解析 JSON 字符串

c++ - 热键、快捷键和加速键有什么区别?

c++ - DWORD 中的包装值

javascript - Windows 应用商店应用程序 : Dynamically load JavaScript for easier update?

html - WinJS:显示软键盘时出现滚动问题

c# - ASP.NET/MVC 1.0 组件和 Ajax 控件 - 网格 - 日历 - 搜索?

windows - 什么是装载机锁?

c# - 如何在Windows应用商店应用程序中获取图像的一个像素(C#)