我想实现函数编辑并在Windows Phone 10中以编程方式添加联系人。
有可能吗?有关于它的任何样品吗?
答案 0 :(得分:4)
以下是用于创建联系人的代码段:
public async Task AddContact(String FirstName, String LastName)
{
var contact = new Windows.ApplicationModel.Contacts.Contact();
contact.FirstName = FirstName;
contact.LastName = LastName;
//Here you can set other properties...
//Get he contact store for the app (so no lists from outlook and other stuff will be in the returned lists..)
var contactstore = await Windows.ApplicationModel.Contacts.ContactManager.RequestStoreAsync(Windows.ApplicationModel.Contacts.ContactStoreAccessType.AppContactsReadWrite);
try
{
var contactLists = await contactstore.FindContactListsAsync();
Windows.ApplicationModel.Contacts.ContactList contactList;
//if there is no contact list we create one
if (contactLists.Count == 0)
{
contactList = await contactstore.CreateContactListAsync("MyList");
}
//otherwise if there is one then we reuse it
else
{
contactList = contactLists.FirstOrDefault();
}
await contactList.SaveContactAsync(contact);
}
catch
{
//Handle it properly...
}
}
以下是更改现有联系人的简短示例:
//you can obviusly couple the changes better then this... this is just to show the basics
public async Task ChangeContact(Windows.ApplicationModel.Contacts.Contact ContactToChange, String NewFirstName, String NewLastName)
{
var contactStore = await Windows.ApplicationModel.Contacts.ContactManager.RequestStoreAsync(Windows.ApplicationModel.Contacts.ContactStoreAccessType.AppContactsReadWrite);
var contactList = await contactStore.GetContactListAsync(ContactToChange.ContactListId);
var contact = await contactList.GetContactAsync(ContactToChange.Id);
contact.FirstName = NewFirstName;
contact.LastName = NewLastName;
await contactList.SaveContactAsync(contact);
}
非常重要: 在appxmanifest中,您必须添加联系人功能。在解决方案资源管理器中右键单击它,然后查看代码"然后在Capabilities put
下<uap:Capability Name="contacts" />
没有这方面的用户界面。请参阅this。
这两个样本都是为了起点......很明显,它不是生产准备好的,你必须根据你的情况进行调整。
由于评论中出现了这一点,我稍微扩展了我的答案。
基于this(加上我自己的实验),聚合联系人的ContactListId为空(如果您考虑它,这是有道理的)。以下是如何与ContactlLstId进行原始联系(代码基于链接中的注释)
public async Task IterateThroughContactsForContactListId()
{
ContactStore allAccessStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly);
var contacts = await allAccessStore.FindContactsAsync();
foreach (var contact in contacts)
{
//process aggregated contacts
if (contact.IsAggregate)
{
//here contact.ContactListId is "" (null....)
//in this case if you need the the ContactListId then you need to iterate through the raw contacts
var rawContacts = await allAccessStore.AggregateContactManager.FindRawContactsAsync(contact);
foreach (var rawContact in rawContacts)
{
//Here you should have ContactListId
Debug.WriteLine($"aggregated, name: {rawContact.DisplayName }, ContactListId: {rawContact.ContactListId}");
}
}
else //not aggregated contacts should work
{
Debug.WriteLine($"not aggregated, name: {contact.DisplayName }, ContactListId: {contact.ContactListId}");
}
}
}
另一件重要的事情:
根据documentation,您将无法更改其他应用创建的所有联系人。
AllContactsReadWrite:
对所有应用和系统联系人的读写权限。这个值是 不适用于所有应用。您的开发者帐户必须是专门的 由Microsoft提供,以请求此级别的访问权限。
在某些情况下,调用SaveContactAsync(contact)时会收到System.UnauthorizedAccessException。这方面的一个例子是联系人在Skype联系人列表中。