(Windows Phone 10) Can I edit, add new contacts programmatically in Windows Phone 10?

I want to implement an editing function and add a contact programmatically in Windows Phone 10.

Is it possible? Does he have a sample?

+4
source share
1 answer

Here is the code snippet for creating a 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 example to modify 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);
}

And very important: In the appx manifest, you must add contact capabilities. Right-click on it in the solution explorer and "View Code", and then in the "Features" section, put

<uap:Capability Name="contacts" />

There is no user interface for this. Cm. .

... , , .

Update

, .

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

}

:

, .

AllContactsReadWrite:

. . Microsoft .

System.UnauthorizedAccessException SaveContactAsync (contact). , Skype.

+3

Source: https://habr.com/ru/post/1623195/


All Articles