ContactManager.RequestStoreAsync()引发System.UnauthorizedAccessException [英] ContactManager.RequestStoreAsync() throws 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!