Udostępnij za pośrednictwem


ContactPickerUI Class

Definition

Allows you to call the contact picker UI so you can select one or more contacts.

public ref class ContactPickerUI sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
class ContactPickerUI final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
public sealed class ContactPickerUI
Public NotInheritable Class ContactPickerUI
Inheritance
Object Platform::Object IInspectable ContactPickerUI
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)
App capabilities
contactsSystem

Examples

This example code prepares the page to use ContactPickerUI:

ContactPickerUI contactPickerUI = MainPagePicker.Current.contactPickerUI;
CoreDispatcher dispatcher = Window.Current.Dispatcher;

public ContactPickerPage()
{
    this.InitializeComponent();
    ContactList.ItemsSource = contactSet;
    ContactList.SelectionChanged += ContactList_SelectionChanged;
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    contactPickerUI.ContactRemoved += contactPickerUI_ContactRemoved;
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    contactPickerUI.ContactRemoved -= contactPickerUI_ContactRemoved;
}

async void contactPickerUI_ContactRemoved(ContactPickerUI sender, ContactRemovedEventArgs args)
{
    // The event handler may be invoked on a background thread, so use the Dispatcher to run the UI-related code on the UI thread.
    string removedId = args.Id;
    await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        foreach (SampleContact contact in ContactList.SelectedItems)
        {
            if (contact.Id == removedId)
            {
                ContactList.SelectedItems.Remove(contact);
                OutputText.Text += "\n" + contact.DisplayName + " was removed from the basket";
                break;
            }
        }
    });
}

This example code shows how to add a contact to the basket with the AddContact(Contact) method.

switch (contactPickerUI.AddContact(contact))
{
    case AddContactResult.Added:
        // Notify the user that the contact was added
        OutputText.Text = contact.DisplayName + " was added to the basket";
        break;
    case AddContactResult.AlreadyAdded:
        // Notify the user that the contact is already added
        OutputText.Text = contact.DisplayName + " is already in the basket";
        break;
    case AddContactResult.Unavailable:
    default:
        // Notify the user that the basket is unavailable
        OutputText.Text = contact.DisplayName + " could not be added to the basket";
        break;
}

This example code shows how to remove a contact from the basket and respond to its removal.

foreach (SampleContact removed in e.RemovedItems)
{
    if (contactPickerUI.ContainsContact(removed.Id))
    {
        contactPickerUI.RemoveContact(removed.Id);
        OutputText.Text = removed.DisplayName + " was removed from the basket";
    }
}

Remarks

To see an example of how to use this class, check out our code sample.

Properties

DesiredFields

Specifies the fields that you want returned after the user selects one or more contacts.

Note

DesiredFields may be altered or unavailable for releases after Windows 8.1. Instead, use DesiredFieldsWithContactFieldType.

DesiredFieldsWithContactFieldType

Gets the fields with contact field type that you want returned after the user selects one or more contacts.

SelectionMode

Determines the selection mode for the contact picker. The most common options are PickSingleContactAsync or PickMultipleContactsAsync.

Methods

AddContact(Contact)

Adds a Contact.

AddContact(String, Contact)

Adds a Contact.

Note

AddContact may be altered or unavailable for releases after Windows 8.1. Instead, use AddContact without the ID.

ContainsContact(String)

Checks to see whether the contact was already selected by the user.

RemoveContact(String)

Removes a contact.

Events

ContactRemoved

Occurs when the user deselects or removes the contact.

Applies to