【问题标题】:(Windows Phone 10) Is possible to edit, add new contact programmatically in windows phone 10?(Windows Phone 10) 是否可以在 Windows Phone 10 中以编程方式编辑、添加新联系人?
【发布时间】:2016-01-07 04:26:44
【问题描述】:

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

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

【问题讨论】:

  • 对于 Windows Phone 10,你的意思是?
  • 是的,对于 windows phone 10,目前我只知道编辑由我的应用程序创建的联系人的方式,我想编辑我设备中存在的联系人

标签: windows uwp


【解决方案1】:

这是用于创建联系人的代码 sn-p:

 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。

这两个示例都是作为起点......显然它还没有准备好生产,您必须根据您的场景对其进行调整。

更新

由于这出现在 cmets 中,我稍微扩展了我的答案。

基于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 联系人列表中时。

【讨论】:

  • 我使用了您的示例代码,但是我从设备获取的联系人对象的 ContactListId 为空,因此无法获取联系人列表对象。你有什么解决办法吗?
  • 您如何获得联系方式? (粘贴代码...)它可能是聚合联系人吗? ...在这种情况下,它可以为空。有关解决方法,请参阅 PatrikAndersson 2015 年 8 月 27 日上午 7:51 的评论social.msdn.microsoft.com/Forums/vstudio/en-US/…
  • 我已经使用您的链接中的源代码来获取联系人列表 ID,我可以得到它,但是当我调用更改联系人时,应用程序在 var contact = await contactList.GetContactAsync(ContactToChange.Id); 处崩溃。要获取联系人列表,我按照此链接中的步骤操作blogs.msdn.com/b/apendo/archive/2015/09/23/…
  • 您的两个 cmets 有点混乱...所以您是否获得了contactList?为什么它与 GetContactAsync 一致? (抛出哪个异常?)。另外,您不应该使用我的 ChangeContact 方法...使其适应您的代码...这只是一个示例。顺便说一句,我更新了我的答案,其中介绍了如何处理聚合的 cmets(再次调整它!)。
  • 我已将 IterateThroughContactsForContactListId() 函数改编为我的代码,并且我的应用程序在 getcontactasync() 时没有崩溃,但在 SaveContactAsync() 时,执行“访问被拒绝。(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED) )" 出现,可能是因为我的应用程序无法访问该联系人(我已将&lt;Capabilities&gt; &lt;uap:Capability Name="contacts"/&gt; &lt;/Capabilities&gt; 添加到我的代码中)。我已阅读您有关“contactstoreaccesstype”的文档,因此如果我没有 Microsoft 专门提供,我无法编辑联系人属于另一个应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多