Working With The Contact Picker In Universal Window App

Prerequisites-

  • Visual Studio 2015

Now, let's get started with the steps, given below-

Step 1 - Create Windows Universal Project

Open Visual Studio 2015 and click File -> New -> Project Option for New Universal app.

New

Step 2 - Giving the Project Name

New Project Window will open. You can select an Installed -> Template -> Visual C# -> Windows -> Universal and select a Blank app (Universal Windows).

Type Project Name ContactPicker and click OK button.

ContactPicker

Here, we choose the Target Version, Minimum Version for our Universal Windows Application and click OK button.

Version

Step 3 - Choose Designer Window

Now, we go to the Solution Explorer and select MainPage.xaml.

Solution Explore

Step 4 - Designing the App

Drag the button from the tool box and change the name to pick a contact in the Property Window.

Drag the button

Next, add Textblock from the tool box and change the text property value to empty in the Property Window.

Textblock

Step 5 - Add the Coding

Next, add the namespaces, given below, to our project, which is required in the further process.

Coding

  1. using System.Text;   
  2. using Windows.ApplicationModel.Contacts;  
Now, we want to bind Contact Picker value to TextBlock. Thus, add the code, given below, to the button click event and it will open the contact list.

Coding
  1. private async void btnContactPicker_Click(object sender, RoutedEventArgs e)   
  2. {  
  3.     var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();  
  4.     contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);  
  5.     Contact contact = await contactPicker.PickContactAsync();  
  6.     var result = new StringBuilder();  
  7.     result.AppendFormat("Name: {0}", contact.DisplayName);  
  8.     result.AppendLine();  
  9.     foreach(ContactPhone phone in contact.Phones) {  
  10.         result.AppendFormat("Phone: {0}", phone.Number);  
  11.         result.AppendLine();  
  12.     }  
  13.     textBlock.Text = result.ToString();  
  14. }  
Step 6 - Run the Application

Now, we are ready to run our project. Thus, click the Local Machine to run the Application.

Output

Application

Now, we can select the contact from Contact Picker Control.

Contact Picker Control

Conclusion

I hope you understood Contact Picker Control in Universal Window and how to run it.

 


Similar Articles