ContactManager.RequestStoreAsync() 抛出 System.UnauthorizedAcc

0

本文介绍了ContactManager.RequestStoreAsync() 抛出 System.UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

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

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

在以前的版本中,此功能仅适用于 Windows Phone 设备.微软文档只是说它现在需要一个 Windows 10 设备系列,但我没有任何运气.

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

推荐答案

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

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.

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

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

这篇关于ContactManager.RequestStoreAsync() 抛出 System.UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14