我正在尝试在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!