【问题标题】:ContactManager.RequestStoreAsync() throws System.UnauthorizedAccessExceptionContactManager.RequestStoreAsync() 抛出 System.UnauthorizedAccessException
【发布时间】:2015-10-22 07:42:20
【问题描述】:

我正在尝试在 Windows 10 通用应用程序 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;
    }

【问题讨论】:

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


    【解决方案1】:

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

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

    <uap:Capability Name="contacts" />
    

    【讨论】:

      【解决方案2】:

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

      <uap:Capability Name="contacts" />
      

      是manifest中需要的关键东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-05
        • 1970-01-01
        相关资源
        最近更新 更多