gpt4 book ai didi

windows - (Windows Phone 10)是否可以在 Windows Phone 10 中以编程方式编辑、添加新联系人?

转载 作者:可可西里 更新时间:2023-11-01 14:02:51 26 4
gpt4 key购买 nike

我想在 Windows Phone 10 中以编程方式实现功能编辑和添加联系人。

这可能吗?有 sample 吗?

最佳答案

下面是创建联系人的代码片段:

 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 中,您必须添加联系人功能。在解决方案资源管理器和“查看代码”中右键单击它,然后在“功能”下放置

<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:

Read and write access to all app and system contacts. This value is not available to all apps. Your developer account must be specially provisioned by Microsoft in order to request this level of access.

在某些情况下,调用 SaveContactAsync(contact) 时我会收到 System.UnauthorizedAccessException。一个例子是当联系人在 Skype 联系人列表中时。

关于windows - (Windows Phone 10)是否可以在 Windows Phone 10 中以编程方式编辑、添加新联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34647386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com