(Windows Phone 10) 是否可以在 Windows Phone 10 中以编程方式编辑、添加新联系人? [英] (Windows Phone 10) Is possible to edit, add new contact programmatically in windows phone 10?

查看:24
本文介绍了(Windows Phone 10) 是否可以在 Windows Phone 10 中以编程方式编辑、添加新联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在windows phone 10中实现功能编辑和以编程方式添加联系人.

I want to implement function edit and add contact programatically in windows phone 10.

有可能吗?有关于它的样本吗?

Is it possible? Has any sample about it ?

推荐答案

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

Here is a code snippet for creating the contact:

 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... 
    }
}

这是更改现有联系人的简短示例:

And here is a short sample for changing an existing contact:

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

And very important: In the appxmanifest you have to add the contacts capability. Right click to it in the solution explorer and "View Code" and then under Capabilities put

<uap:Capability Name="contacts" />

没有用于此的用户界面.请参阅.

There is no UI for this. See this.

这两个示例都是作为起点...显然它还没有准备好生产,您必须使其适应您的场景.

Both samples are meant to be for starting point... obviously it's not production ready and you have to adapt it to your scenario.

既然这在评论中出现,我就稍微扩展一下我的答案.

Since this came up in the comments I extend my answer a little bit.

基于 this(加上我自己的实验)聚合联系人的 ContactListId 为空(如果您考虑一下,这是有道理的).以下是如何使用 ContactlLstId 获取原始联系人(代码基于链接中的评论)

Based on this (plus my own experimentation) the ContactListId for aggregated contacts is null (which makes sense if you think about it). Here is how to get the raw contact with ContactlLstId (code is based on the comment from the link)

 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}");
                }
            }

}

还有一个重要的事情:

根据文档 您将无法更改由其他应用程序创建的所有联系人.

According to the documentation you won’t be able to change all the contacts which are created by other apps.

AllContactsReadWrite:

AllContactsReadWrite:

对所有应用和系统联系人的读写权限.这个值是并非适用于所有应用程序.您的开发者帐户必须特别由 Microsoft 提供以请求此级别的访问权限.

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 联系人列表中时.

In some cases, I get a System.UnauthorizedAccessException when SaveContactAsync(contact) is called. One example for this was when the contact was in the Skype Contact List.

这篇关于(Windows Phone 10) 是否可以在 Windows Phone 10 中以编程方式编辑、添加新联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆