我想在Windows Phone 10中以编程方式实现功能编辑和添加联系人。
可能吗?有样品吗?
这是用于创建联系人的代码段:
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...
}
}
Run Code Online (Sandbox Code Playgroud)
以下是更改现有联系人的简短示例:
//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);
}
Run Code Online (Sandbox Code Playgroud)
非常重要:在appxmanifest中,您必须添加联系人功能。在解决方案资源管理器和“查看代码”中右键单击它,然后在“功能”下
<uap:Capability Name="contacts" />
Run Code Online (Sandbox Code Playgroud)
没有为此的UI。看到这个。
这两个样本都是为了作为起点……显然,它还没有准备好生产,因此您必须使其适应您的情况。
由于在评论中提到了这一点,因此我扩大了回答范围。
基于此(加上我自己的实验),聚合联系人的ContactListId为null(如果考虑一下,这是有道理的)。这是获取与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}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
还有一件重要的事情:
根据文档,您将无法更改由其他应用创建的所有联系人。
AllContactsReadWrite:
对所有应用程序和系统联系人的读写访问权限。此值不适用于所有应用程序。您的开发者帐户必须由Microsoft专门提供,才能请求此级别的访问权限。
在某些情况下,调用SaveContactAsync(contact)时会出现System.UnauthorizedAccessException。例如,当联系人位于Skype联系人列表中时。
| 归档时间: |
|
| 查看次数: |
1077 次 |
| 最近记录: |