[英]ContactManager.RequestStoreAsync() throws System.UnauthorizedAccessException
I am trying to use the ContactManager class in the Windows 10 Universal apps API. 我试图在Windows 10 Universal apps API中使用ContactManager类。 I am trying to do this on a Windows 10 Desktop machine.
我试图在Windows 10桌面计算机上执行此操作。
I am receiving an exception, "System.UnauthorizedAccessException" when trying to request a list of contacts using ContactManager.RequestStoreAsync(). 我在尝试使用ContactManager.RequestStoreAsync()请求联系人列表时收到异常“System.UnauthorizedAccessException”。
In previous versions, this function only worked on Windows Phone devices. 在以前的版本中,此功能仅适用于Windows Phone设备。 The Microsoft documentation just says it requires a Windows 10 Device family now, but I'm not having any luck.
微软文档说它现在需要一个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;
}
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.
看起来如果您手动将“联系人”功能添加到Package.appxmanifest文件,它将解决问题。
There is no UI option for this capability. 此功能没有UI选项。 You have to somehow know it exists, edit the file in a text editor instead of in the UI, and add:
您必须以某种方式知道它存在,在文本编辑器中而不是在UI中编辑文件,并添加:
<uap:Capability Name="contacts" />
As of 2018, there is such capability listed on Capabilities tab when visually editing the Package.appxmanifest , at least in VS2017. 截至2018年, 有功能选项卡上列出当目视编辑Package.appxmanifest,至少在VS2017这样的能力。 Anyway, the
无论如何,
<uap:Capability Name="contacts" />
is the crucial thing required in manifest. 是清单所需的关键事项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.