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

标签 windows uwp

我想在 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/

相关文章:

C - 省略了参数名称?

c - 为什么 void 指针的大小在 Windows 64 位平台上是 4

c++ - 使用 GDI+ 与 HTML 页面

Windows DNS 服务器调试日志主机名格式

c# - 如何使用通用 Windows 应用程序中的 BackgroundMediaPlayer 播放内部声音

c# - 如何通过 UWP 应用正确着色灰度图像?

windows - 延迟执行 IRP_MN_SET_POWER

javascript - UWP:获取剪贴板文本

f# - 使用F#时,当前是否不可能将UWP应用发布到Store?

uwp - 禁用然后启用时,RichEditBox 会丢失颜色格式