ContactPicker For Windows 10

Here we are using ContactPicker to get the information of the contact.

Step 1: Open a blank app and add a Button and a TextBlock either from the toolbox or by copying the following XAML code into your grid.

  1. <StackPanel Margin="20,20,0,0">  
  2. <TextBlock Text="Contact Picker" FontSize="20"></TextBlock>  
  3. <StackPanel Margin="10" >  
  4. <Button Name="contactPicker" Content="Pick a contact" Height="40" Width="130" Click="contactPicker_Click" ></Button>  
  5. <TextBlock Name="selectedContact" Margin="10,20,0,0" Height="50" FontSize="20"></TextBlock>  
  6. </StackPanel>  
  7. </StackPanel>  
  8.      
 

Step 2: Add the following namespaces to your project which is needed in further C# code.

  1. using System.Text;  
  2. using Windows.ApplicationModel.Contacts;  

Step 3: Copy and paste the following code to the cs page which will be called on button click event and will open the contact list.

  1. private async void contactPicker_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     var contactPicker = new ContactPicker();  
  4.     contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);  
  5.   
  6.     Contact contact = await contactPicker.PickContactAsync();  
  7.   
  8.     var result = new StringBuilder();  
  9.     result.AppendFormat("Name: {0}", contact.DisplayName);  
  10.     result.AppendLine();  
  11.   
  12.     foreach (ContactPhone phone in contact.Phones)  
  13.     {                 
  14.         result.AppendFormat("Phone: {0}", phone.Number);  
  15.         result.AppendLine();  
  16.     }  
  17.   
  18.     selectedContact.Text = result.ToString();  
  19. }  

Step 4: Run your application and test yourself.

 


Similar Articles